0

I have a customer page where I can add/update a customer. On this page I have a partial view with it's own form to add/update a customer address. I put this in a partial view because I want to reuse it on other pages. When I open the modal and try to post from the form in the partial view, I would expect it to post to my AddressController but it hits my CustomerController.

Is it possible to do what I am trying to do? Or do I need to use ajax to call my method. If that is the case I'm not sure how to handle my json token, so I was hoping I can make it work like this.

Thanks!

Main Page

@model MyProject.Models.CustomerViewModel
<form asp-action="Create">
    <div class="row">
        <div class="col-md-6">
            <h2 class="icon-customer">New Customer</h2>
        </div>
        <div class="col-md-6">
            <div class="form-group pull-right">
                <input type="submit" value="Create" class="btn btn-default" />
                <a asp-action="Index" class="btn btn-default">Back to List</a>
            </div>
        </div>
    </div>
    <div class="spacer"></div>
    <div class="form-horizontal">
        <div class="form-group">
            <label asp-for="Customer.FirstName" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Customer.FirstName" class="form-control" />
                <span asp-validation-for="Customer.FirstName" class="text-danger"></span>
            </div>
        </div>
    </div>
</form>
<div id="addAddress">Add Address</div>
<div id='modalAddress' class='modal'>
    <div class="modal-dialog">
        <div class="modal-content">
            @Html.Partial("../Address/Create.cshtml", Model.AddressViewModel)
        </div>
    </div>
</div>

Partial View (/Address/Create.cshtml)

@model MyProject.Models.AddressViewModel
<form asp-controller="Address" asp-action="Create">
    <div class="row">
        <div class="col-md-6">
            <h2 class="icon-address">New Address</h2>
        </div>
        <div class="col-md-6">
            <div class="form-group pull-right">
                <button id="btnClose" type="button" data-dismiss="modal" aria-hidden="true">X</button> 
                <input type="submit" value="Create" class="btn btn-default" />
                <a asp-action="Index" class="btn btn-default">Back to List</a>
            </div>
        </div>
    </div>
    <div class="spacer"></div>
    <div class="form-horizontal">
        <div class="form-group">
            <label asp-for="Address.Address1" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Address.Address1" class="form-control" />
                <span asp-validation-for="Address.Address1" class="text-danger"></span>
            </div>
        </div>
    </div>
</form>
Primico
  • 2,143
  • 3
  • 24
  • 36
  • How is this partial view form rendered in your .html? You can check "
    " part.
    – Yigitalp Ertem Oct 21 '16 at 05:50
  • Nested forms are invalid html and not supported. But why would you even have 2 forms? (you can only submit one form at a time and all the edits the user made in the other form would be lost so this makes no sense) –  Oct 21 '16 at 06:04
  • @StephenMuecke the forms don't seem nested in the code, they are separate. – Yigitalp Ertem Oct 21 '16 at 06:51
  • @yalpertem, Your right (missed the closing `` tag). OP has not shown us the relevant code because it would go to `AddressController` based on code shown –  Oct 21 '16 at 07:22
  • Let's say I have a customer edit page. At the top is the customer form and at the bottom is a list of customer addresses. A user can update the customer using the form at the top, or the user can click to open a modal to add or edit an address from the list at the bottom. The top posts to the CustomerController, and the bottom needs to call an action in the AddressController. I know having 2 forms doesn't make much sense, but I am not quite sure how to make it work. In the past, with Viewstate, you could add/edit addresses at the bottom without losing data from the customer form. – Primico Oct 21 '16 at 13:41
  • 1
    You should be doing this inside one form and allow the user to dynamically add (and remove) addresses as required and post it all back in one action - refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) for some examples –  Oct 21 '16 at 22:04
  • Stephen, thanks for the suggestion. I used one form, but I decided to use ajax to handle the addresses. So I make ajax calls to add/edit/delete addresses, and then I refresh the page from on success to get the new view model and everything is working great. Thanks. – Primico Oct 25 '16 at 03:09

0 Answers0