Bastyon
Bastyon

Reputation: 1611

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.

<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. The Model object is null

Object reference not set to an instance of an object.

anyone see what Im missing here?

Upvotes: 0

Views: 3085

Answers (2)

SenaReddy
SenaReddy

Reputation: 38

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

Upvotes: 1

Aravindan
Aravindan

Reputation: 855

You have to pass the model in Header from the view and from controller and in partialview too lisk below

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

Upvotes: 1

Related Questions