开发者

jquery dialog button to do the same as asp.net mvc submit button

I have tried to figure out a way of letting a jquery dialog button called Create do the same as an asp.net submit button.

<input type="submit" value="Create" />

I am using jquery 1.3.2

I have come up with the following to let the dialog use the correct controller method.

       var url = '<%= Url.Action("Create1", "Home") %>';
       $.post(url,data, 
                            function(data) {
                                alert("Successful. Id for this client is " + data.ClientNo);
                                $("#CreateForm input").attr("value", ""); // Success
                            },
                            "json"); // DataType  

However, the method requires a model parameter

    [Acc开发者_开发问答eptVerbs(HttpVerbs.Post)]
    public ActionResult Create1(ClientDetail client)

Looking at http://api.jquery.com/jQuery.post/ data parameter is a map or string sent to the server

I am wondering is it possible to convert a model into a map or string possibly using the .attr method?

Thank you


The only thing you have to do is make sure that your post data has the same names as the property values of ClientDetail. So if ClientDetail has two properties: Name and Age, then make sure that the data you post looks like this:

var data = { Name: 'ClientName', Age: 24 };

ASP.NET MVC's DefaultModelBinder will bind the posted data to your ClientDetail object.


See http://api.jquery.com/submit/

<form id="target" action="destination.html">
  <input type="text" value="Hello there" />
  <input type="submit" value="Go" />
</form>
<div id="other">
  Trigger the handler
</div>

Jquery function:

$('#other').click(function() {
  $('#target').submit();
});


What I can suggest you to do is : let the signature accept all your parameters, and you on the server side will construct your object, and save it to your data store.

consider the following one:

    [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create1(string clientName, string clientTitle, int clientAge)
    {         
           var client = new Client()
                         {
                           name = clientName, 
                           title = clientTitle, 
                           age = clientAge, 
                           .., 
                           .., etc
                         } 
    }

let me know if this works for you, and don't forget to mark as answer if it really helped you

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜