Razor 编辑页面时排除modelview中某些参数的最佳方法
我知道在modelview中有几个选项可以排除/包括一些参数,比如使用绑定或使用接口。然而,当我试图实现嵌套的IEnumerable变量时,我遇到了一些问题。例如:Razor 编辑页面时排除modelview中某些参数的最佳方法,razor,model-view-controller,viewmodel,Razor,Model View Controller,Viewmodel,我知道在modelview中有几个选项可以排除/包括一些参数,比如使用绑定或使用接口。然而,当我试图实现嵌套的IEnumerable变量时,我遇到了一些问题。例如: public class TestViewModel() { public int Id { get; set; } public IEnumerable<Organisation> KPI { get; set; } } public class Organisation { public
public class TestViewModel()
{
public int Id { get; set; }
public IEnumerable<Organisation> KPI { get; set; }
}
public class Organisation
{
public string Id { get; set; }
public string Name {get; set;}
public DateTime StartDate {get; set;}
public IEnumerable<Regiod> CategoryValues { get; set; }
}
public class Region
{
public System.Guid Id { get; set; }
public System.Int32 RegionId { get; set; }
public int Value { get; set; }
public System.String RegionName { get; set; }
}
[HttpGet]
public ActionResult edit(int id)
{
var model = new TestViewModel();
// Do something to populate the model
view(model)
}
一切正常,除非有人使用例如fiddler来设置其他禁用或隐藏的值,所以这些字段将被更新
我要做的是更新刚刚启用的字段并排除其余字段,即使有人试图为其设置值
我尝试绑定[Exclude and Include],但我的问题是我可以绑定来自不同类的2个值。我尝试了UpdateModel(model,include),但没有成功
任何建议都将不胜感激。防止过度发布的最佳方法是,创建一个视图模型,该模型只包含视图所需的属性。查看一下,您应该使用
组织
和地区
的视图模型。提交时,初始化数据模型并仅映射所需视图模型中的属性。谢谢各位,如果我将编辑操作方法更改为:public ActionResult edit(int-id,organization model1,Region model2),它应该可以工作吗?我刚刚创建了一个视图模型,它只包含那些需要的属性,然后在编辑操作方法中映射它们,它工作得很好。再次感谢各位
[HttpPost]
public ActionResult edit(TestViewModel model)
{
// Do something to populate the model
}