为什么我的Modal表单将空模型发布回控制器

时间:2014-11-14 01:51:33

标签: c# jquery asp.net asp.net-mvc twitter-bootstrap

我有一个部分视图,我在一个模态中加载..在索引视图中使用HTML.Partial的模型div 看起来像这样。

<div class="modal fade" id="modalEditDBInfo" role="application" aria-labelledby="modalEditDBInfoLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modalEditDBInfoContent" style="background-color:white; border-radius:10px; box-shadow:10px;">
            @Html.Partial("_EditDatabaseInfo")
        </div>
    </div>
</div>

部分视图代码是

@model Hybridinator.Domain.Entities.Database
<br />
<br />

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <h4 class="modal-title" id="editModelTitle">Edit Database Info</h4>
</div>
<div class="modal-body">
    @using (Html.BeginForm("EditDatabaseInfo", "Database", FormMethod.Post, new { @class = "modal-body" }))
    {   
            <div class="form-group">
                <div id="databaselabel" >@Html.LabelFor(m => m.database, "Database")</div>
                <div id="databaseedit" >@Html.EditorFor(m => m.database)</div>
            </div>
            <div class="form-group">
                <div id="databaseserverlabel" >@Html.LabelFor(m => m.database_server, "Database Server")</div>
                <div id="databaseserveredit" >@Html.EditorFor(m => m.database_server)</div>
            </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button class="btn btn-inverse btn-primary" type="submit">Save</button>
    </div>
    }       
</div>

如果成功触发此控制器

 [HttpPost]
       public ActionResult EditDatabaseInfo(Database database)
       {
           string s = database.database;
           //do other stuff
           return RedirectToAction("Index");
       }

一切正常,直到我尝试访问应该传递给ActionResult方法的控制器帖子中的模型。 Model对象为null

Object reference not set to an instance of an object

有人看到我在这里失踪的东西吗?

2 个答案:

答案 0 :(得分:1)

您必须从视图和控制器以及部分视图中传递Header中的模型 lisk下面

请深入了解** **

之间的粗体文字和文字
**@model Hybridinator.Domain.Entities.Database**

<div class="modal fade" id="modalEditDBInfo" role="application" aria-labelledby="modalEditDBInfoLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modalEditDBInfoContent" style="background-color:white; border-radius:10px; box-shadow:10px;">
        @Html.Partial("_EditDatabaseInfo", **Model** )
    </div>
</div>




[HttpPost]
   public ActionResult EditDatabaseInfo(Database database)
   {
       string s = database.database;
       //do other stuff
       // **you have to get the model value in here and pass it to index action**
       return RedirectToAction(**"Index", modelValue**);
   }



 public ActionResult Index(**ModelClass classValue**)
   {
      //pass the model value into index view.
       return View(**"Index", classValue**);
   }

答案 1 :(得分:1)

在视图,部分视图和操作中更改模型。而不是传递实体模型,创建视图模型并将其传递到视图和部分视图中。请考虑以下

@model **DatabaseModel**

<div class="modal fade" id="modalEditDBInfo" role="application" aria-labelledby="modalEditDBInfoLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modalEditDBInfoContent" style="background-color: white; border-radius: 10px; box-shadow: 10px;">
            @Html.Partial("_EditDatabaseInfo", **Model**)
        </div>
    </div>
</div>

@model DatabaseModel

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <h4 class="modal-title" id="editModelTitle">Edit Database Info</h4>
</div>
<div class="modal-body">
    @using (Html.BeginForm( new { @class = "modal-body" }))
    {   
            <div class="form-group">
                <div id="databaselabel" >@Html.LabelFor(m => m.DatabaseName, "Database")</div>
                <div id="databaseedit" >@Html.EditorFor(m => m.DatabaseName)</div>
            </div>
            <div class="form-group">
                <div id="databaseserverlabel" >@Html.LabelFor(m => m.DatabaseServer, "Database Server")</div>
                <div id="databaseserveredit" >@Html.EditorFor(m => m.DatabaseServer)</div>
            </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button class="btn btn-inverse btn-primary" type="submit">Save</button>
    </div>
    }       
</div>

public class DatabaseModel
{
    public string DatabaseName { get; set; }
    public string DatabaseServer { get; set; }
}

据我所知,数据库是一个关键词,因为它变为空