ASP.net MVC - 为POST操作独立视图模型 [英] ASP.net MVC - Separate ViewModel for POST Action

查看:170
本文介绍了ASP.net MVC - 为POST操作独立视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC应用我有类似于这样的视图模型:

 公共类ComplexViewModel
{
    公共ComplexDetailsViewModel点评详情{搞定;组; }
    公共ComplexDetailsViewModel Details2 {搞定;组; }
}公共类ComplexDetailsViewModel
{
    公众诠释标识{搞定;组; }
    公共字符串DisplayValue1 {搞定;组; }
    公共字符串DisplayValue2 {搞定;组; }
    // ...
}

我本来在做我的看法如下:

  @ Html.HiddenFor(型号=> model.Details1.Id)
@ Html.HiddenFor(型号=> model.Details2.Id)@ Html.DisplayFor(型号=> model.Details1.DisplayValue1)
...

我会后整个模型到控制器:

 公众的ActionResult邮政(ComplexViewModel模型)

我实际上并不需要除了ID值从ComplexViewModel任何东西,所以我决定创建专门用于张贴数据使用的另一种视图模型:

 公共类PostViewModel
{
    公众诠释Details1Id {搞定;组; }
    公众诠释Details2Id {搞定;组; }
}公众的ActionResult邮政(PostViewModel模型)

问题是,现在我的 @HiddenFor(型号=> model.Details1.Id)不那么实际并没有任何POST方法映射到我的Post模型

有没有办法让我的POST模式和GET我的模型分离式结构,同时还使用 HiddenFor 帮手?


解决方案

只要写的HTML中隐藏的输入,而不是使用HTML辅助手。

 <输入类型=隐藏ID =Details1IdVALUE =@ Model.Details1.Id/>
<输入类型=隐藏ID =Details2IdVALUE =@ Model.Details1.Id/>

更新

我不得不做类似的问题。我结束了我的看法不上不下的形式相关的属性。 Automapper 使得它很容易从其他物体到您的视图地图,并且可以扁平化层次。这样做,你的新看法可能最终会寻找与此类似。

 公共类ComplexViewModel
{
    众长Details1Id {搞定;组; }
    公共字符串Details1Name {搞定;组; }
    众长Details2Id {搞定;组; }
    公共字符串Details2Name {搞定;组; }
}

In my MVC application I have a View Model that looks similar to this:

public class ComplexViewModel
{
    public ComplexDetailsViewModel Details1 { get; set; }
    public ComplexDetailsViewModel Details2 { get; set; }
}

public class ComplexDetailsViewModel 
{
    public int Id { get; set; }
    public string DisplayValue1 { get; set; }
    public string DisplayValue2 { get; set; }
    // ...
}

I was originally doing the following in my view:

@Html.HiddenFor(model => model.Details1.Id)
@Html.HiddenFor(model => model.Details2.Id)

@Html.DisplayFor(model => model.Details1.DisplayValue1)
...

I would POST the full model to the controller:

 public ActionResult Post(ComplexViewModel model)

I don't actually need anything from ComplexViewModel except for the Id values, so I decided to create another view model used specifically for POSTing the data:

public class PostViewModel
{
    public int Details1Id { get; set; }
    public int Details2Id { get; set; }   
}

public ActionResult Post(PostViewModel model)

The problem is that now my @HiddenFor(model => model.Details1.Id) does not map to my POST model so nothing actually gets POSTed.

Is there a way to have the separate structure for my POST model and my GET model while still using the HiddenFor helper?

解决方案

Just write the HTML for the hidden inputs by hand instead of using the Html Helpers.

<input type="hidden" id="Details1Id" value="@Model.Details1.Id"/>
<input type="hidden" id="Details2Id" value="@Model.Details1.Id"/>

Update

I had issues doing something similar. I ended up flattening out the form related properties on my views. Automapper makes it really easy to map from other objects to your view and can flatten out hierarchies. Doing this, your new view might end up looking similar to this.

public class ComplexViewModel        
{        
    public long Details1Id { get; set; }        
    public string Details1Name { get; set; }
    public long Details2Id { get; set; }    
    public string Details2Name { get; set; }    
}  

这篇关于ASP.net MVC - 为POST操作独立视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆