Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please how do i get input values from a bootstrap modal popup from mvc 5 method, i am calling the method from the view using this:

HTML
<button type="button" class="btn btn-blue" onclick="location.href = '@Url.Action("SaveToDb")';return false;">


And the method is inside a controller, below is my method:

C#
public void SaveToDb(AddressViewModel addrs)
    {
        var addressLine1 = addrs.AddressLine1;
        var addressLine2 = addrs.AddressLine2;
        var city = addrs.City;

    }



i have a break point at the entry of SaveToDb method, but AddressViewModel object (addrs) is alway null. please any assistance will be appreciated.


My Modal Popup form:

HTML
<form role="form" class="form-horizontal" method="POST">
    <!-- start: BOOTSTRAP EXTENDED MODALS -->

    <div id="responsive" class="modal fade" tabindex="-1" data-width="760" style="display: none; removed 50px;">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                ×
            </button>
            <h4 class="modal-title">Add Address</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                @Html.LabelFor(model => model.ClientId, htmlAttributes : new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ClientId, new { htmlAttributes = new { @class = "form-control", @ReadOnly = true } })
                    @Html.ValidationMessageFor(model => model.ClientId, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.AddressLine1, htmlAttributes : new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.AddressLine1, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.AddressLine1, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.AddressLine2, htmlAttributes : new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.AddressLine2, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.AddressLine2, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.City, htmlAttributes : new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
                </div>
            </div>
            
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" class="btn btn-light-grey">
                    Close
                </button>
                <button type="button" class="btn btn-blue" onclick="location.href = '@Url.Action("Savetodb")';return false;">
                    Save changes
                </button>
            </div>
        </div>
    </div>
    <!-- ends: BOOTSTRAP EXTENDED MODALS -->
</form>
Posted
Updated 17-Nov-15 20:42pm
v3

Try with below code:
HTML
<form role="form" class="form-horizontal" method="POST" id="testForm" action="Home/About">
<!-- Add your controls here -->
...............
<button type="button" data-dismiss="modal" class="btn btn-light-grey">Close</button>
<button type="submit" id="btnSubmit" class="btn btn-blue">
        onclick="$('#testForm').submit()">Save Changes</button>
</form>

Here I defined id and action attribute in form tag. In action attribute it is defined "ControllerName/ActionName" pattern - you need to change as per your need. Then on button click just submitting the form(make sure jQuery library is added in page).
 
Share this answer
 
v3
Comments
Krunal Rohit 18-Nov-15 4:30am    
void method won't work for that matter. It should be an Action method.

-KR
[no name] 18-Nov-15 4:42am    
That means you are telling that above code won't fire Controller's action. Please elaborate if I am wrong.
First, make the button type submit-
HTML
<button type="submit" class="btn btn-blue">Save

Second, give action to form-
HTML
<form role="form" action="@Url.Action("SaveToDb", "ControllerName")" class="form-horizontal" method="POST">

Third, attribute the method as HttpPost and should be ActionResult-
C#
[HttpPost]
public ActionResult SaveToDb(AddressViewModel addrs)
{
    var addressLine1 = addrs.AddressLine1;
    var addressLine2 = addrs.AddressLine2;
    var city = addrs.City;

    return View("SomeView");
}


Now, put a breakpoint on the very first line of the SaveToDb method and see if control is actually getting here or not. And make sure that the model used in form is same as used in method.

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900