0

我有两个视图,创建和编辑。两者都有一个隐藏字段ModelType,用于在我的模型绑定器中使用它来绑定所有子类。

此隐藏字段在编辑视图中工作正常,但在创建视图中不工作。我正在null reference exception排队:

@Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName) 

在创建视图中。

这里有什么问题?

编辑.cshtml

@using PartyBiz.Models.Objects
@model Organization

@using (Html.BeginForm("Edit", "Organization", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Edit Organization</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.C) 
        @Html.TextBoxFor(model => model.C, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.C) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.N)
        @Html.TextBoxFor(model => model.N, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.N)
    </div> <br />
    <div class="editor-label">
        @Html.LabelFor(model => model.D)
        @Html.TextBoxFor(model => model.D, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.D)
    </div> 
    <br />
        @Html.HiddenFor(model=> model.PID)
        @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)
        <input type="submit" value="Edit" />

</fieldset>
}  

创建.cshtml

@using PartyBiz.Models.Objects
@model Organization

@using (Html.BeginForm("Create", "Organization", FormMethod.Post))
{

@Html.ValidationSummary(true)
<fieldset>
    <legend>Create a New Organization</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.C) 
        @Html.TextBoxFor(model => model.C, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.C) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.N)
        @Html.TextBoxFor(model => model.N, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.N)
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.D)
        @Html.TextBoxFor(model => model.D, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.D)
    </div> 
    <br />
        <input type="submit" value="Create" />
        @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)

</fieldset>
}  

模型粘合剂

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ValueProvider.ContainsPrefix("ModelType"))
        {
            //get the model type
            var typeName = (string)bindingContext
                .ValueProvider
                .GetValue("ModelType")
                .ConvertTo(typeof(string));
            var modelType = Type.GetType(typeName);

            //tell the binder to use it
            bindingContext.ModelMetadata =
                ModelMetadataProviders
                .Current
                .GetMetadataForType(null, modelType);
        }
        return base.BindModel(controllerContext, bindingContext);
    }
4

1 回答 1

0

我的猜测是你没有在Create方法中传递任何值作为你的模型。您的 create 方法可能如下所示:

public ActionResult Create()
{
    return View();
} 

这将导致Model属性null位于 Razor 模板中。要解决此问题,您可以传入模型类的默认实例:

public ActionResult Create()
{
    return View(new Organization());
}
于 2013-06-25T07:53:55.617 回答