Saturday 15 May 2010

html - ASP.NET MVC Dynamic Forms -


Can ASP suggest a good way to develop dynamic forms with .NET MVC?

I have a cascading dropdown on the page (depending on the option value in the dropdown, selected in the previous dropdown).

All values ​​come from the database

How can I apply this kind of behavior using ASP.NET MVC?

Of course I would like to get all the values ​​of the controller while submitting my form.

If you need some dynamic field in your form, the best way for you is to have some advanced JavaScript Frameworks should be used like, etc.

If you need anything more or less simple then you are enough to use knockout. I recommend angular for more advanced scenarios, but this is my personal preference.

Here is a simple implementation of a dynamic form with a knockout:

= div = "snippet" data-lang = "JS" data-hide = " Wrong ">
  var model = {user: koassabelEre (), educator: function ( } {This.users.push ({name: ko.observable ()}); }}; Ko.applyBindings (model);   
  & lt; Script src = "https: //cdnjs.cloudflare com / ajax / libs / knockout / 3.2.0 / knockout-min.js" & gt; & Lt; / Script & gt; & Lt; Div data-bind = "foreach: user" & gt; & Lt; Input type = "text" data-bind = "value: name" /> gt; & Lt; Br / & gt; & Lt; / Div & gt; & Lt; Button data-bind = "click: addUser" & gt; Add user & lt; / Button & gt; & Lt; Ul Data-Bind = "Future: Users" & gt; & Lt; Li data-bind = "text: name" & gt; & Lt; / Li & gt; & Lt; / Ul & gt;     

Now, what about ASP.NET MVC?

This is more difficult. Perhaps the best and easiest way would be to post JSON for AXX and ASP.NET MVC action. First of all, you need to get the JSON object from your form. It's pretty easy to beat:

  var json = ko.toJSON (model);   

Now, when we know how to get a JSON from the form, the next step is to send your data to an action. Perfect for:

  $ Ajax ({type: "POST", url: "@ Url.Action (" AddUser "), data: To.Tosason (model)., // Serialize for JSON and accept user array: 'Application / Jason ', Success: function (data) {warning ("done!")}} // your success callback});   

In our case, we basically send an array of stars, thus ASP.NET MVC action should look like this:

  [http post ] Public Jesenstein Advuser (list & lt; string & gt; user) {back Jason (data); // return some}   

This is definitely not the only option to implement dynamic forms, but I think it is very effective. Hope it helps.

No comments:

Post a Comment