从传递到一个局部视图嵌套复杂对象获取的值 [英] getting the values from a nested complex object that is passed to a partial view
问题描述
我有了一个复杂的对象作为其成员之一的视图模型。复杂的对象有4个属性(所有字符串)。我试图创建一个可重复使用的部分观点,我可以在复杂的对象传递,并将它生成HTML辅助其属性的HTML。这是所有伟大的工作。然而,当我提交表单,模型绑定不映射值回视图模型的成员,所以我没有得到任何东西在服务器端。我怎样才能读取的值用户键入到HTML佣工对于复杂的对象。
I have a ViewModel that has a complex object as one of its members. The complex object has 4 properties (all strings). I'm trying to create a re-usable partial view where I can pass in the complex object and have it generate the html with html helpers for its properties. That's all working great. However, when I submit the form, the model binder isn't mapping the values back to the ViewModel's member so I don't get anything back on the server side. How can I read the values a user types into the html helpers for the complex object.
视图模型
public class MyViewModel
{
public string SomeProperty { get; set; }
public MyComplexModel ComplexModel { get; set; }
}
MyComplexModel
MyComplexModel
public class MyComplexModel
{
public int id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
....
}
控制器
public class MyController : Controller
{
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.ComplexModel = new MyComplexModel();
model.ComplexModel.id = 15;
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// model here never has my nested model populated in the partial view
return View(model);
}
}
查看
@using(Html.BeginForm("Index", "MyController", FormMethod.Post))
{
....
@Html.Partial("MyPartialView", Model.ComplexModel)
}
管窥
@model my.path.to.namespace.MyComplexModel
@Html.TextBoxFor(m => m.Name)
...
我怎么可以绑定表单提交该数据,以便父模型中包含数据的Web表单上从局部视图中输入?
how can I bind this data on form submission so that the parent model contains the data entered on the web form from the partial view?
感谢
编辑:我想通了,我需要prePENDComplexModel。我所有的控制在局部视图(文本框),因此它映射到嵌套的对象,但我不能视图模型类型传递到局部视图,以获得额外的层,因为它需要是通用接受几个视图模型名称类型。我可以只重写与JavaScript的name属性,但似乎过于贫民窟给我。我还能怎么办呢?
I've figured out that I need to prepend "ComplexModel." to all of my control's names in the partial view (textboxes) so that it maps to the nested object, but I can't pass the ViewModel type to the partial view to get that extra layer because it needs to be generic to accept several ViewModel types. I could just rewrite the name attribute with javascript, but that seems overly ghetto to me. How else can I do this?
编辑2:我可以静态设置新的{名称=ComplexModel.Name} name属性,所以我觉得我的业务,除非有人有更好的方法
EDIT 2: I can statically set the name attribute with new { Name="ComplexModel.Name" } so I think I'm in business unless someone has a better method?
推荐答案
您可以通过preFIX到部分使用
You can pass the prefix to the partial using
@Html.Partial("MyPartialView", Model.ComplexModel,
new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ComplexModel" }})
这将perpend的preFIX你的控件名称
属性,这样<输入名称=名称../>
将成为<输入名称=ComplexModel.Name../& GT;
并正确绑定到的typeof MyViewModel
上回发
which will perpend the prefix to you controls name
attribute so that <input name="Name" ../>
will become <input name="ComplexModel.Name" ../>
and correctly bind to typeof MyViewModel
on post back
修改
要使它更容易一些,你可以在一个HTML辅助封装这一
To make it a little easier, you can encapsulate this in a html helper
public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName)
{
string name = ExpressionHelper.GetExpressionText(expression);
object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
var viewData = new ViewDataDictionary(helper.ViewData)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = name }
};
return helper.Partial(partialViewName, model, viewData);
}
和使用它作为
@Html.PartialFor(m => m.ComplexModel, "MyPartialView")
这篇关于从传递到一个局部视图嵌套复杂对象获取的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!