要求是将用户输入从视图传递到控制器,并且视图模型属性的类型将在运行时决定。这是我的代码:
模型结构
public class MyModel
{
public Device Device { get; set; }
}
public class Device
{
public string Name { get; set; }
public string Vender { get; set; }
}
public class Mobile : Device
{
public string SimsSlots { get; set; }
}
控制器
public class MyController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult AddDevice()
{
return View();
}
[HttpPost]
public ActionResult SaveDevice(MyModel model)
{
//Save the Device
return Content("Saved");
}
}
查看
@model mvcFirst.Models.MyModel
<form action="/My/SaveDevice" method="post">
<table cellspacing="5" cellpadding="5">
<tr>
<th>
Name
</th>
<td>
@Html.TextBoxFor(m=>m.Device.Name)
</td>
</tr>
<tr>
<th>
Vender
</th>
<td>
@Html.TextBoxFor(m=>m.Device.Vender)
</td>
</tr>
<tr>
<th>
Number of sim slots
</th>
<td>
@Html.TextBoxFor(m=>(m.Device as mvcFirst.Models.Mobile).SimsSlots)
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
我没有找到任何方法让我的应用程序能够确定保存的输入是移动设备。