[英]Retrieve Value Partial View and Pass to Controller
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.
当用户选择客户时,他们将在视图中的LoadConfiguration按钮上计时。
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");
}
}
}
Based on your setup, it looks like your partial view will POST back to the Index action. 根据您的设置,您的部分视图似乎将回发到Index操作。 I would add another action method like so
我会添加另一个类似的操作方法
[HttpPost]
public ActionResult Index(int CustomerId)
{
//process
Return View("Load")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.