简体   繁体   English

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

[英]Why is my Modal form posting a null model back to the controller

I have a partial view that I load in a Modal..in the index view the model div with the HTML.Partial looks like this. 我有一个局部视图,我在Modal ..中加载了索引视图,其中带有HTML.Model的模型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>

the partial view code is 部分视图代码是

@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>

If fire this controller successfully 如果成功触发此控制器

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

Everything works fine up until I try to access the model in the controller post which should be passed into ActionResult method. 在我尝试访问控制器帖子中的模型之前,一切工作正常,应该将其传递到ActionResult方法中。 The Model object is null Model对象为null

Object reference not set to an instance of an object . Object reference not set to an instance of an object

anyone see what Im missing here? 有人看到我在这里缺少什么吗?

You have to pass the model in Header from the view and from controller and in partialview too lisk below 您必须从视图和控制器的Header中传递模型,并且在下面的局部视图中传递模型

Please get look deeply in bold text and the text in between ** ** 请深入了解粗体文本和介于** **之间的文本

**@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**);
   }

Change the Model in view, partial view and in action . 在视图,局部视图和操作中更改模型。 Instead of passing entity model, create view model and pass it in view as well as partial view. 与传递实体模型不同,创建视图模型并将其传递给视图和局部视图。 consider the following 考虑以下

 @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; }
}

As of my knowledge Database is a key word, because of that it is getting null 据我所知数据库是一个关键词,因为它正变得空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM