codingdir logo sitemap sitemap |

Send a model to MVC3 controller by setting src of element

By : , Category : c#

Try to use an html form with it's target attribute set to the name of your iframe, something like:

<iframe style="display: none;" id="myIframe" name="myIframe"></iframe>

var f = document.createElement("form");
f.method = "POST";
f.action = "../Workflow/ExportPatchSchedules"; = "myIframe";
f.enctype = "application/x-www-form-urlencoded"; // not sure about this since you didn't mention

var input = document.createElement('input');
input.type = "hidden"; = "model";
input.value = "...." // your data here, not sure about it since you didn't mention

ReLated :

It's a Model Bind List, in this case, you have to send the information using the same name of your parameter in the name attribute of html inputs tag and model binder will convert it on a collection on the post. Your javascript looks fine. Try something like this:

In the view:

<input type="text" name="roles" value="1" />
<input type="text" name="roles" value="4" />
<input type="text" name="roles" value="2" />
<input type="text" name="roles" value="8" />

in the controller:

public ActionResult Post(List<int> roles) 
    // process

Also take a look at this article:

It depends what you wish to do with the model on the server, and what you wish to return. Do you wish to return a partial view or just something indicating how the submission went?

You have several options.

You can send the model using Ajax form

@using (Ajax.BeginForm("getPT", new AjaxOptions { HttpMethod="POST", UpdateTargetId = "UpdateContainer" }))
    <!-- FORM elements goes here... -->

in your view, place

<div id="UpdateContainer"></div>

Where you want to render the partial view

Instead of returning ActionResult, set the action to return PartialViewResult

public PartialViewResult getPT(ReportModel repModel)
    // instantiate a viewmodel
    var model = new YourViewModel();
    return PartialView("YourPartial", model);

Or, if you don't need it to return a partial view, maybe you just need to know how the submission went, you can send it with jQuery, like you are attempting in your code, to a WEB API controller

public class ApiTestController : ApiController
    public bool getPT(ReportModel model)
        // do the work with your model
        return ..... whatever ? :)

Your ajax call

  var json= "{"isSuccess": false,    "Errors": {   "e794bfa7-657b-482c-acdf-c5a3b6d2ce83": [        {              "ErrorMsg": "Invalid object name 'asfasf.CONTACT'.",      "Id": "63755f2a-1f02-480e-ab94-cafc62fd9634",             "ScriptContent": "CREATE VIEW [asfasf].Blala AS select * from [asfasf].[CONTACT]",            "ScriptDescription": "Script 6",      "ServerType": "SQLSERVER",       "Success": false      }     ]  }}";

        type: "POST",
        url: '/Default1/DisplayErrorsReportAsync?json=' + json,
        data: json,
        dataType: 'json',
        //contentType: false,
        //processData: false,
        success: function (response) {
        error: function (error) {

Take json as string in your controller

in controller use

  public ActionResult DisplayErrorsReportAsync(string aaa)
    JObject o = JObject.Parse(json);
    JArray a = (JArray)o["ErrorReportModel"];
    ErrorReportModel model= a.ToObject<ErrorReportModel>();

Note : use Newtonsoft.Json

In order for the model binder to work you need to have the default constructor defined:

So add this to your class and you wont have to use FormCollection

public class EditLeaveForm
    public EditLeaveForm() { }
    public EditLeaveForm(int application_id) : base()

Also make sure that all the properties you want to bind to have getters and setters.


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .