我有一个允许用户选择客户的部分视图。当用户选择客户时,他们将在视图中的LoadConfiguration按钮上计时。
我希望视图将所选客户传递给控制器操作方法,以便在加载文件时可以在逻辑中使用它。
有人可以建议最好的方法来执行此操作到目前为止我的代码如下:
部分视图
@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>
}
查看
@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>
控制器
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");
}
}
}
答案 0 :(得分:1)
根据您的设置,您的局部视图看起来会POST回到Index操作。我会添加另一个像这样的动作方法
[HttpPost]
public ActionResult Index(int CustomerId)
{
//process
Return View("Load")
}