Tutorial :Passing the model in a view back to an action



Question:

I am wondering how I can pass a model back to an action so that I can continue to work on the data but in a different action based on the button's pressed

To send it back, I specify the Control, Action and use the new { dom = Model } to specify the parameter.

dom is a List (so a list of domain objects. My model passed in is an IQueryable. When I make dom an IQueryable, I still receive nothing back from the view. here is the snippet, using Telerik controls

VIEW

<% Html.Telerik().Grid(Model).Name("Domains")          .DataKeys(dataKeys => dataKeys.Add(c => c.DomainId)).DataKeys(dataKeys => dataKeys.Add(c => c.Timestamp))          .Columns(columns =>              {                  columns.Template(o =>                   {  %>          <%= Html.Encode(Html.OutputAction(ViewData["PerformActions"] as List<string>))%>          <%              }).Title("Action");                  columns.Bound(o => o.DomainId);                  columns.Bound(o => o.Name);                  columns.Bound(o => o.SiteId);                  columns.Bound(o => o.ScrubAndRedirect);                  columns.Bound(o => o.ReportingSiteId);                  columns.Bound(o => o.TrafficCopClass);                  columns.Bound(o => o.SiteName);                  columns.Bound(o => o.FeedType);                  columns.Bound(o => o.Active);              }).Pageable().Sortable().Filterable().DataBinding(db => db.Server().Select("Domains", "Preview", new { doms = Model })).Render();%>      *ACTION*     public ActionResult Preview(List<Domain> doms)   {       return View("Preview", doms.AsQueryable<Domain>());   }  

Thanks


Solution:1

I think what you want to do is just have your Preview and Commit actions both take in List doms. If preview doesn't post it, you may need to have an EditorForModel somewhere (even if it's hidden). In other words, your edit view posts to the preview action, which shows a view, and then THAT page should post the data to your commit action. I believe that would do it. Hope that helps..


Solution:2

In general in MVC, you do not pass large lists of DOM objects back to the server to "re-construct" a list of entity or domain objects. When values are posted to a Controller action, they are almost always from an HTML form. Behind the scenes, MVC is extracting the values from the POST via the

Request.Forms["fieldName"]  

And then trying to auto-map the values to params in your ActionMethod.

If I understand your scenario, the most common practice would be to submit or pass a KEY (or some identifier) to the Controller when an action is fired and then use that to grab the necessary domain objects (either from a cache, from a database, from disk, etc.). If scalability is a concern, a good caching strategy can help avoid costly I/O operations.

Hope that helps.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »