Question

I have a partial view that allows a user to select a customer. When the user selects the customer they will clock on the LoadConfiguration button in the view.

I want the view to pass the selected customer to the controller action method so that I can use it in my logic when loading the files.

Can someone advise the best way to do this my code so far is below:

Partial View

    @model Mojito.Models.Customer

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerId, "Customer", new {@class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.DropDownList("CustomerId", String.Empty)
                @Html.ValidationMessageFor(model => model.CustomerId)
            </div>

        </div>
    </div>
}

View

    @using Mojito.Models
@model Mojito.Models.MojitoXmlConfiguration


@{
    ViewBag.Title = "Mojito Load Config";
}


<div>@Html.Partial("~/Views/Shared/_Customer.cshtml")</div>
@using (Html.BeginForm("Load", "MojitoXmlConfiguration", FormMethod.Post))
{
    @Html.AntiForgeryToken()


    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">

                <label for="file">Filename:</label>
                <input type="file" name="file" id="file" />
                <input type="submit" value="Load Mojito Configuration" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Controller

    using System.Web.Mvc;
using Mojito.Models;

namespace Mojito.Controllers
{
    public class MojitoXmlConfigurationController : Controller
    {
        private MojitoContext _db = new MojitoContext();
        //
        // GET: /MojitoXmlConfiguration/
        public ActionResult Index()
        {
            ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
            return View();
        }
        [HttpPost]
        public ActionResult Load()
        {
            var mojitoXml = new MojitoXmlConfiguration.Importer(@"C:\Users\Documents\XML Files\SampleList");
            mojitoXml.ImportWsaHolidayUsingXElement();

            ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
            return View("Index");
        }
    }
}
Was it helpful?

Solution

Based on your setup, it looks like your partial view will POST back to the Index action. I would add another action method like so

[HttpPost]
public ActionResult Index(int CustomerId)
{
   //process
   Return View("Load")

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top