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

时间:2012-07-18 21:33:05

标签: c# html asp.net-mvc-3 http-post html-post

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

HTML

<script type="text/javascript">
$(document).ready(function () {
    $("#SavetreventLocationLookupAddButton").click(function () {
        $("#addSaveTreventLocationLookup").submit();
    });
});

添加Trevent位置查找

<form id="addSaveTreventLocationLookup" method="post" action="<%: Url.Action("AddSaveTreventLocationLookup","Prod") %>">
    <table>
        <tr>
            <td colspan="3" class="tableHeader">Trevent Location Lookup Detail</td>
        </tr>
         <tr>
            <td colspan="2" class="label">Acquiring Institution Identification Code:</td>
            <td class="content">
                <input type="text" maxlength="200" name="AcquiringInstitutionIdentificationCode" id="AcquiringInstitutionIdentificationCode" />
            </td>
        </tr>
         <tr>
            <td colspan="2" class="label">Card Acceptor Identification Code:</td>
            <td class="content">
                <input type="text" maxlength="200" name="CardAcceptorIdentificationCode" id="CardAcceptorIdentificationCode" />
            </td>
        </tr>
         <tr>
            <td colspan="2" class="label">Merchant Id:</td>
            <td class="content">
                <input type="text" maxlength="200" name="MerchantId" id="MerchantId" />
            </td>
        </tr>
        <tr>
            <td colspan="3" class="tableFooter">
                    <br />
                    <a id ="SavetreventLocationLookupAddButton" href="#" class="regularButton">Add</a>
                    <a href="javascript:history.back()" class="regularButton">Cancel</a>
            </td>
        </tr>
    </table>
</form>

位指示

[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");
    }

4 个答案:

答案 0 :(得分:1)

创建一个像这样的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将负责将请求值绑定到模型。无论如何,您应该阅读有关模型绑定器和验证的内容,以获得一个想法。

答案 1 :(得分:0)

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

像这样:

public ActionResult AddSaveTreventLocationLookup(FormCollection formCollection)
{

// now you can get the values you want
     string acquiringInstitutionIdentificationCode = formCollection["AcquiringInstitutionIdentificationCode"];
.......

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

//还要注意,因为我有剃刀引擎你会注意到我的观点正在使用    // @ html.TextBoxFor ...但是,如果你没有剃刀引擎...那么你会使用    //类似于&lt;%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

<form id="addSaveTreventLocationLookup" method="post"action="@Url.Action("AddSaveTreventLocationLookup", "Test")">
<table>
    <tr>
        <td colspan="3" class="tableHeader">Trevent Location Lookup Detail</td>
    </tr>
    <tr>
        <td colspan="2" class="label">Acquiring Institution Identification Code:</td>
        <td class="content">
            @*<input type="text" maxlength="200"   name="AcquiringInstitutionIdentificationCode" id="AcquiringInstitutionIdentificationCode" />*@
            @Html.TextBoxFor(m=>m.AcquiringInstitutionIdentificationCode , new {maxlength="200"})
        </td>
    </tr>
    <tr>
        <td colspan="2" class="label">Card Acceptor Identification Code:</td>
        <td class="content">
            @*<input type="text" maxlength="200" name="CardAcceptorIdentificationCode" id="CardAcceptorIdentificationCode" />*@
            @Html.TextBoxFor(m => m.CardAcceptorIdentificationCode, new { maxlength = "200" })
        </td>
    </tr>
    <tr>
        <td colspan="2" class="label">Merchant Id:</td>
        <td class="content">
            @* <input type="text" maxlength="200" name="MerchantId" id="MerchantId" />*@
            @Html.TextBoxFor(m => m.MerchantId, new { maxlength = "200" })
        </td>
    </tr>
    <tr>
        <td colspan="3" class="tableFooter">
            <br />
            <a id ="SavetreventLocationLookupAddButton" href="#" class="regularButton">Add</a>
            <a href="javascript:history.back()" class="regularButton">Cancel</a>
        </td>
    </tr>
</table>
</form>
 <script type="text/javascript">
$(document).ready(function () {
    $("#SavetreventLocationLookupAddButton").click(function () {
        $("#addSaveTreventLocationLookup").submit();
    });
});
  </script>

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