0

我正在尝试从我的 html 代码中获取数据,例如“acquingCode”、“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

4 回答 4

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 将为您处理将请求值绑定到模型。无论如何,您都应该阅读有关模型绑定器和验证的内容以了解想法。

于 2012-07-18T21:54:32.943 回答
0

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

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

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

于 2012-07-18T22:10:22.520 回答
0

//另请注意,因为我有剃须刀引擎,您会注意到我的视图正在使用 // @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; }
  }
于 2012-07-18T22:18:45.453 回答
0

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

像这样:

public ActionResult AddSaveTreventLocationLookup(FormCollection formCollection)
{

// now you can get the values you want
     string acquiringInstitutionIdentificationCode = formCollection["AcquiringInstitutionIdentificationCode"];
.......
于 2012-07-18T21:54:16.830 回答