Ken*_*ker 1 c# razor asp.net-mvc-3
在MVC3 Razor中:
我正在尝试使用来自多个对象的字段动态创建表单.但由于某种原因,我在控制器中获得的数据不包含输入值.
FormViewModel.cs
namespace DynamicForm.Models
{
public class FormViewModel
{
public Name name = new Name();
public Address address = new Address();
public FormViewModel()
{
}
}
public class Name
{
[Required()]
public String first { get; set; }
[Required()]
public String last { get; set; }
public Name()
{
first = "";
last = "";
}
}
public class Address
{
public String street1 { get; set; }
public String street2 { get; set; }
public Address()
{
street1 = "";
street2 = "";
}
}
}
Run Code Online (Sandbox Code Playgroud)
FormController.cs
[HttpPost()]
public ActionResult Save(FormViewModel toSave)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
index.cshtml:
@using DynamicForm;
@using DynamicForm.Models;
@model FormViewModel
@{
ViewBag.Title = "Form";
}
<h2>Form</h2>
@using (Html.BeginForm("Save", "Form"))
{
@Html.TextBoxFor(m => m.address.street1)
@Html.TextBoxFor(m => m.address.street2)
@Html.TextBoxFor(m => m.name.first)
@Html.TextBoxFor(m => m.name.last)
<input type="submit" value="Send" />
}
Run Code Online (Sandbox Code Playgroud)
有关为什么数据没有填充到FormViewModel对象的任何想法?
在FormViewModel中,名称和地址应该是属性. 默认模型绑定器仅适用于属性.
public class FormViewModel
{
public Name Name {get;set;}
public Address Address {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
858 次 |
| 最近记录: |