如何使用method =“post”从表单中获取数据? 如何在我的控制器中请求数据?

我试图从我的HTML代码中获取数据,如“acquringCode”,“cardAcceptor”和“merchantId”。 我无法想象如何在我的控制器中获取该数据。 我知道它的request.form。 我相信我做错了。 有没有更简单的方法让我通过函数传递对象或每个名称作为参数?

HTML

 $(document).ready(function () { $("#SavetreventLocationLookupAddButton").click(function () { $("#addSaveTreventLocationLookup").submit(); }); }); 

添加Trevent位置查找

 <form id="addSaveTreventLocationLookup" method="post" action=""> 
Trevent Location Lookup Detail
Acquiring Institution Identification Code:
Card Acceptor Identification Code:
Merchant Id:

Add Cancel

位指示

 [HttpPost] [AuthorizeAttribute(AdminRoles = "AddTreventLocationLookup")] public ActionResult AddSaveTreventLocationLookup() { try { string acquiringInstitutionIdentificationCode; //= Request.Form["AcquiringInstitutionIdentificationCode"] ?? string.Empty; string cardAcceptorIdentificationCode;// =/Request["CardAcceptorIdentificationCode"] ?? string.Empty; string merchantId;// = Request["MerchantID"] ?? string.Empty; if (!string.IsNullOrEmpty(Request.Form["AcquiringInstitutionIdentificationCode"])) { acquiringInstitutionIdentificationCode = Request.Form["AcquiringInstitutionIdentificationCode"]; } if (!string.IsNullOrEmpty(Request.Form["CardAcceptorIdentificationCode"])) { cardAcceptorIdentificationCode = Request.Form["CardAcceptorIdentificationCode"]; } if (!string.IsNullOrEmpty(Request.Form["MerchantID"])) { merchantId = Request.Form["MerchantID"]; } AdminProductionServices.TreventLocationLookup treventLocationLookup = Administrator.Models.AdminProduction.TreventLocationLookup.loadTreventLocationLookup(Guid.Empty, Guid.Empty, string.Empty, string.Empty, string.Empty)[0]; treventLocationLookup.acquiringInstitutionIdentifcationCode = acquiringInstitutionIdentificationCode; treventLocationLookup.cardAcceptorIdentificationCode = cardAcceptorIdentificationCode; treventLocationLookup.merchantId = merchantId; Administrator.Models.AdminProduction.TreventLocationLookup.addTreventLocationLookup(treventLocationLookup); } catch(Exception e) { Commons.ErrorHandling.ReportError("Administrator.Controller.ProdController AddSaveTreventLocationLookup()", e); } return RedirectToAction("SearchTreventLocationLookup", "Prod"); } 

创建一个像这样的viewModel:

 public class TreventLocationLookupViewModel { public string InstitutionIdentificationCode {get; set;} public string CardAcceptorIdentificationCode {get; set;} public string MerchantId {get; set;} } 

然后在你的Action中使用它:

 public ActionResult AddSaveTreventLocationLookup(TreventLocationLookupViewModel model) { AdminProductionServices.TreventLocationLookup treventLocationLookup = Administrator.Models.AdminProduction.TreventLocationLookup.loadTreventLocationLookup(Guid.Empty, Guid.Empty, string.Empty, string.Empty, string.Empty)[0]; treventLocationLookup.acquiringInstitutionIdentifcationCode = model.InstitutionIdentificationCode; treventLocationLookup.cardAcceptorIdentificationCode = model.CardAcceptorIdentificationCode; treventLocationLookup.merchantId = model.MerchantId; Administrator.Models.AdminProduction.TreventLocationLookup.addTreventLocationLookup(treventLocationLookup); return RedirectToAction("SearchTreventLocationLookup", "Prod"); } 

MVC将负责将请求值绑定到模型。 您应该阅读有关模型绑定器和validation的信息,以获得一个想法。

请尝试将FormCollection参数添加到操作AddSaveTreventLocationLookup

像这样:

 public ActionResult AddSaveTreventLocationLookup(FormCollection formCollection) { // now you can get the values you want string acquiringInstitutionIdentificationCode = formCollection["AcquiringInstitutionIdentificationCode"]; ....... 

veblock的答案是完全正确的。 但您也可以绑定到操作中的简单变量。

 public ActionResult AddSaveTreventLocationLookup(string InstitutionIdentificationCode, string CardAcceptorIdentificationCode, string MerchantId) { ..code.. } 

只要您的表单字段与变量命名相同,MVC将在查找request.form,request.querystring和路由变量中的可能源之后为您绑定它们。

//还要注意,因为我有剃刀引擎你会注意到我的视图正在使用// @ html.TextBoxFor …但是如果你没有剃刀引擎……那么你会使用//像<%Html.TextBoxFor这样的东西。 //

//控制器逻辑

  [HttpPost] public ActionResult AddSaveTreventLocationLookup(TreventModel model) { string acquiringInstitutionIdentificationCode; string cardAcceptorIdentificationCode; string merchantId; acquiringInstitutionIdentificationCode = model.AcquiringInstitutionIdentificationCode; cardAcceptorIdentificationCode = model.CardAcceptorIdentificationCode; merchantId = model.MerchantId; // return RedirectToAction("TreventLookUp"); } public ActionResult TreventLookUp() { return View("TreventLookUp"); } } 

//查看逻辑

  @model MvcApplication2.Models.TreventModel 
Trevent Location Lookup Detail
Acquiring Institution Identification Code: @**@ @Html.TextBoxFor(m=>m.AcquiringInstitutionIdentificationCode , new {maxlength="200"})
Card Acceptor Identification Code: @**@ @Html.TextBoxFor(m => m.CardAcceptorIdentificationCode, new { maxlength = "200" })
Merchant Id: @* *@ @Html.TextBoxFor(m => m.MerchantId, new { maxlength = "200" })

Add Cancel
//View Model public class TreventModel { public string AcquiringInstitutionIdentificationCode { get; set; } public string CardAcceptorIdentificationCode { get; set; } public string MerchantId { get; set; } }