如何获取使用方法=“邮报”的表单数据? 如何在我的控制器请求它的数据?(How to obtai

2019-06-26 09:24发布

我想从像“acquringCode”,“cardAcceptor”和“MERCHANTID”我的HTML代码中获取数据。 我想不出如何如何获得在我的控制器的数据。 我知道它的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");
    }

Answer 1:

创建这样一个视图模型:

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

然后在你的行动那样使用它:

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会照顾绑定请求值的模型为您服务。 你应该有一个关于模型的粘合剂和验证读反正拿个主意。



Answer 2:

通过添加参数的FormCollection的动作,请尽量AddSaveTreventLocationLookup

像这样:

public ActionResult AddSaveTreventLocationLookup(FormCollection formCollection)
{

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


Answer 3:

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

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

只要你的表单字段命名为同样的事情作为变量,MVC将寻找的Request.Form,的Request.QueryString和路由变量可能的来源后绑定它们。



Answer 4:

//还要注意,因为我有剃刀引擎,你会发现我的意见是使用// @ 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

<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; }
  }


文章来源: How to obtain data from a form using method=“post”? How to request it data in my controller?