Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Partial Views Passing Model to Controller

First of all, I should say I am new to MVC, but learning it as I using it in a project.

I currently have a problem, that I think stems from my lack of understanding of MVC. Here is an outline of what I have:

Controller: PersonController

Model: PersonIndexModel PersonFindAddressModel (postcode, list of possible addresses) PersonNewAddressModel (first line, second line, post code)

View: RegisterAddress (uses PersonIndexModel, includes both partial views). FindAddress (uses PersonFindAddressModel) NewAddress (uses PersonNewAddressModel).

What I intend to happen is that when registering, the user puts in a post code. I have a html submit button that then goes back to the Controller, checks the name of the button pressed, and if it is "find postcode" it will pass back a list of possible addresses (using PersonFindAddressModel).

If the user selects that the address is not in the list, it again goes back to the same place as before, checks which button was pressed (in this case, "address not found"), hides the 'Find Address' part, and shows the 'New Address' part.

New address contains normal address forms, and a button to submit. I tried to create a new ActionLink to call something in the PersonController, but it will not.

Where is it I am going wrong? Should partials have controllers?

To be more specific, here is my code:

RegisterAddress

@Html.Partial("FindAddress")
@Html.Partial("NewAddress", new PersonNewAddressModel())

FindAddress

@model PersonFindAddressModel
@using (Html.BeginForm("RegisterAddress", "Person"))
{
if (@ViewBag.NewAddress != true)
{
    @Html.TextBoxFor(m=> m.PostCode)

    <button type="submit" value="findaddress" name="command">Find Address</button>


    if (Model != null && Model.AddressList != null && Model.AddressList.Count > 0)
    {
        <div class="selectproperty">
            @Html.DropDownList("selectaddress", new SelectList(Model.AddressList, "value", "text"))
        </div>

        <div>
            <button type="submit" value="notinlist" name="command"> Not in the list?</button>
        </div>
    }

}

New Address

@model PersonNewAddressModel

@{
    ViewBag.Title = "FindAddress";
}

@using (Html.BeginForm("RegisterAddress", "Person"))
{
 if (@ViewBag.NewAddress == true)
   {
    @Html.TextBoxFor(m=> m.Street)

    @Html.ActionLink("Submit", "SubmitNew")
}
}

PersonController

public class PersonController : Controller
{
[HttpPost]
    public ActionResult RegisterAddress(PersonFindAddressModel model, string command)
    {
        switch (command)
        {
            case "findaddress":
                PostcodeLookup(model);
                break;

            case "notinlist":
                ViewData.Add("NewAddress", true);
                break;

        }

        return View(model);
    }

 public ActionResult SubmitNew(PersonNewAddressModel model)
    {

        return View(model);
    }

Any help would be greatly appreciated!

like image 241
Andy Jones Avatar asked Jun 07 '26 05:06

Andy Jones


1 Answers

You can use the same controller for all the functionality. You don't need separate controllers for your partials.

With regards to your 'one method being called and the name of the button pressed being determined' you don't need to to this. Each button can call a different Action on the Controller. Personally, I would use two forms in the FindAddress View:

@model PersonFindAddressModel
@using (Html.BeginForm("FindAddress", "Person"))
{
    if (@ViewBag.NewAddress != true)
    {
        @Html.TextBoxFor(m => m.PostCode)
        <input type="submit" value="Find" />
    }
}

if (Model.AddressList != null && Model.AddressList.Count > 0)
{
    @using (Html.BeginForm("SaveAddress", "Person")
    {
        <div class="selectproperty">
            @Html.DropDownList("selectaddress", new SelectList(Model.AddressList, "value", "text"))
        </div>

        <div>
            <input type="submit" value="Save" />
            @Html.ActionLink("Not in list","NotInList")
        </div>
    }
}

Your actions for these:

public ActionResult FindAddress(PersonFindAddressModel model)
{
    var address = PostcodeLookup(model);

    // bind found address to PersonFindAddressModel

    return View(model);
}

public ActionResult NotInList()
{
    ViewData.Add("NewAddress", true);
    return RedirectResult("Index");
}

public ActionResult SaveAddress(PersonFindAddressModel model)
{
    // save address from model and do any redirecting ect.
}

For the AddNewAddress View:

@model PersonNewAddressModel

@using (Html.BeginForm("SubmitNewAddress", "Person"))
{
    @Html.TextBoxFor(m => Model.Street)
    <input type="submit" value="Add New" />
}

And then add an action for SubmitNewAddress:

public ActionResult SubmitNew(PersonNewAddressModel model)
{
    // save new address and redirect
}
like image 173
Paul Welbourne Avatar answered Jun 09 '26 02:06

Paul Welbourne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!