How to obtain data from a form using method=“post”? How to request it data in my controller?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 18:16:33

问题


I am trying to obtain data from my html code like the "acquringCode", "cardAcceptor", and "merchantId". I can't figure how how to obtain that data in my controller. I know its request.form. I believe im doing it wrong. Is there an easier way for me to pass the object or each name as a parameter through the function?

Html

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

Add Trevent Location Lookup

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

Contoller

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

回答1:


Create a viewModel like this one:

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

and then use it in your Action like that:

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 will take care of the binding the request values to the model for you. You should have a read about model binders and validation anyway to get an idea.




回答2:


Please try by adding the FormCollection parameter to the action AddSaveTreventLocationLookup

Like this:

public ActionResult AddSaveTreventLocationLookup(FormCollection formCollection)
{

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



回答3:


veblock's answer is totally correct. But you can also bind to simple variables in your action.

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

As long as your form fields are named the same things as the variables, MVC will bind them for you after looking for possible sources in request.form, request.querystring, and routing variables.




回答4:


//Also note since i have the razor engine you will notice my views are using // @html.TextBoxFor...However if you dont have razor engine... then you would be using // Something like <%Html.TextBoxFor. //

//Controller logic

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

// View Logic

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


来源:https://stackoverflow.com/questions/11550707/how-to-obtain-data-from-a-form-using-method-post-how-to-request-it-data-in-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!