简体   繁体   English

将视图中的模型传递回动作

[英]Passing the model in a view back to an action

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. 为了发回它,我指定了Control,Action,并使用新的{dom = Model}指定参数。

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 dom是一个List(因此是一个域对象列表。我传入的模型是IQueryable。当我将dom设置为IQueryable时,仍然看不到任何视图。这是使用Telerik控件的代码段。

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 谢谢

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). 如果没有发布预览,则可能需要在某处放置一个EditorForModel(即使已隐藏)。 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.. 希望有帮助。

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. 通常,在MVC中,您不会将大量DOM对象列表传递回服务器来“重建”实体或域对象列表。 When values are posted to a Controller action, they are almost always from an HTML form. 将值发布到Controller操作后,它们几乎总是来自HTML表单。 Behind the scenes, MVC is extracting the values from the POST via the 在后台,MVC通过以下方式从POST中提取值:

Request.Forms["fieldName"]

And then trying to auto-map the values to params in your ActionMethod. 然后尝试将值自动映射到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.). 如果我了解您的情况,最常见的做法是在触发操作时向控制器提交或传递KEY(或某些标识符),然后使用它来获取必要的域对象(从缓存,数据库中获取) ,磁盘等)。 If scalability is a concern, a good caching strategy can help avoid costly I/O operations. 如果需要考虑可伸缩性,那么好的缓存策略可以帮助避免昂贵的I / O操作。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM