Form POST后,Inner Object ExtraData为NULL。我无法将对象传递给Controller。需要帮助来弄清楚这里有什么问题。
我的模特
public class TestModel
{
public int ClaimNumber { get; set; }
public string Key { get; set; }
public string SaveMessage { get; set; }
public bool UserAuthorizedEdit { get; set; }
public ExtraDataModel EData { get; set; }
}
public class ExtraDataModel
{
public int ID { get; set; }
public string Code { get; set; }
public string Desc { get; set; }
}
我的观点
<h2>View1</h2>
@using (Html.BeginForm("SaveDetails", "SaveData", Model, FormMethod.Post, new { @id = "RenderProviderVForm", target = "_blank" }))
{
<table>
<tr>
<td> @Html.TextBoxFor(model => model.ClaimNumber)</td>
</tr>
<tr>
<td> @Html.DisplayFor(model => model.Key)</td>
</tr>
<tr>
<td> @Html.TextBoxFor(model => model.SaveMessage)</td>
</tr>
<tr>
<td> @Html.TextBoxFor(model => model.EData.ID)</td>
</tr>
<tr>
<td> @Html.TextBoxFor(model => model.EData.Code)</td>
</tr>
<tr>
<td> @Html.TextBoxFor(model => model.EData.Desc)</td>
</tr>
<tr>
<td>
<button type="submit" class="link-button" value="submit">@Model.ClaimNumber</button>
</td>
</tr>
</table>
}
我的控制器
[HttpPost]
public void SaveDetails(TestModel tm)
{
if (ModelState.IsValid)
{
//Save Code
}
else
{
System.Diagnostics.Debug.Write(ModelState);
}
}
ModelState上的错误说:
System.InvalidOperationException:参数转换类型&#39; System.String&#39;输入&#39; MvcApplication2.Models.ExtraDataModel&#39;失败,因为没有类型转换器可以在这些类型之间进 在System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture,Object value,Type destinationType) 在System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture,Object value,Type destinationType) 在System.Web.Mvc.ValueProviderResult.ConvertTo(Type type,CultureInfo culture) 在System.Web.Mvc.ValueProviderResult.ConvertTo(类型类型) 在System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState,String modelStateKey,ValueProviderResult valueProviderResult,Type destinationType)}
我现在遇到了这个例外:
The parameter conversion from type 'System.String' to type 'MvcApplication2.Models.ExtraDataModel' failed because no type converter can convert between these types.
以下是生成的HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>View1</title>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<h2>View1</h2>
<form action="/SaveData/SaveDetails?ClaimNumber=123456&Key=40e686ca-1176-4d45-a1dd-33b79b3a9764&SaveMessage=Save%20Me&UserAuthorizedEdit=True&EData=MvcApplication2.Models.ExtraDataModel" id="RenderProviderVForm" method="post" target="_blank"><table>
<tr>
<td> <input data-val="true" data-val-number="The field ClaimNumber must be a number." data-val-required="The ClaimNumber field is required." id="ClaimNumber" name="ClaimNumber" type="text" value="123456" /></td>
</tr>
<tr>
<td> <input id="Key" name="Key" type="text" value="40e686ca-1176-4d45-a1dd-33b79b3a9764" /></td>
</tr>
<tr>
<td> <input id="SaveMessage" name="SaveMessage" type="text" value="Save Me" /></td>
</tr>
<tr>
<td> <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="EData_ID" name="EData.ID" type="text" value="888888" /></td>
</tr>
<tr>
<td> <input id="EData_Code" name="EData.Code" type="text" value="CD" /></td>
</tr>
<tr>
<td> <input id="EData_Desc" name="EData.Desc" type="text" value="DVD" /></td>
</tr>
<tr>
<td>
<button type="submit" class="link-button" value="submit">123456</button>
</td>
</tr>
</table>
</form>
<script src="/Scripts/jquery-1.8.2.js"></script>
</body>
</html>
现在,如果我必须在表单上编辑并仅发布 ExtraDataModel 值,我应该做些什么修改。