将视图(aspx)中的输入值传递给控制器

时间:2015-02-05 13:38:13

标签: c# asp.net-mvc view controller

是的,有人能帮帮我吗?

我想将两个文本框的输入值传递给视图的控制器。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/personenbeheer.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UDL.Domain.Persoon>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PersonenbeheerContent" runat="server">

    <legend class="form-signin-heading">Personenlijst</legend>
    <% Html.BeginForm("Index", "Persoon", FormMethod.Get);%>
    <% var currentUser = HttpContext.Current.User; %>
    <%  if (currentUser.IsInRole("Beheerder"))
        { %>

    <nav>
        <ul class="nav nav-pills nav-justified" role="tablist">
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Maak persoon aan", "Create", null, new { @class="UDLbutton" })%></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Lijst Pdf", "PrintPersonen", new { sort = ViewData["sort"], zoeknaam = ViewData["zoekNaam"]}, new { target = "_blank",@class="UDLbutton"}) %></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#etiketten"> Adres etiketten</button></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Export emailadressen","ExportEmails", new { sort = "Adres", zoeknaam = ViewData["zoekNaam"]}, new { target = "_blank",@class="UDLbutton"}) %></li>
        </ul>
    </nav>
    <%  } %>


    <% Html.BeginForm("Index", "Persoon", FormMethod.Post);

       { %>

           <input type="text" id="txtA"/>

            <input type="text" id="txtB"/>

            <input type="submit" value="Verzenden"/>

     <%  } %>

正如您所看到的,我使用两次Html.BeginForm用于相同的方法,但一次用于GET,一次用于POST。但是POST没有工作:(

这是控制器:

//
// GET: /Persoon/
[Authorize(Roles = "Gebruiker, Beheerder")]
public ActionResult Index(string sortOrder, string zoekNaam, int? page, string huidigefilter)
{
    //huidige zoekfilter
    ViewBag.huidigzoeken = sortOrder;
    //ViewBags om waarden door te geven naar de View en ze geven data over het sorteren door
    ViewBag.PersoonIDSortParm = String.IsNullOrEmpty(sortOrder) ? "PersoonID_desc" : "";
    ViewBag.NaamSortParm = sortOrder == "Naam" ? "Naam_desc" : "Naam";
    ViewBag.AanhefSortParm = sortOrder == "Aanhef" ? "Aanhef_desc" : "Aanhef";
    ViewBag.GemeenteSortParm = sortOrder == "Gemeente" ? "Gemeente_desc" : "Gemeente";
    ViewBag.GeslachtSortParm = sortOrder == "Geslacht" ? "Geslacht_desc" : "Geslacht";
    ViewBag.AdresSortParm = sortOrder == "Adres" ? "Adres_desc" : "Adres";
    ViewBag.VoornaamSortParm = sortOrder == "Voornaam" ? "Voornaam_desc" : "Voornaam";
    ViewBag.TelefoonSortParm = sortOrder == "Telefoon" ? "Telefoon_desc" : "Telefoon";
    ViewBag.GSMSortParm = sortOrder == "GSM" ? "GSM_desc" : "GSM";
    ViewBag.EmailSortParm = sortOrder == "Email" ? "Email_desc" : "Email";
    ViewBag.PartnerSortParm = sortOrder == "Partner" ? "Partner_desc" : "Partner";
    ViewBag.OpmerkingenSortParm = sortOrder == "Opmerkingen" ? "Opmerkingen_desc" : "Opmerkingen";
    //ViewBag.StatusLidSortParm = sortOrder == "StatusLid" ? "StatusLid_desc" : "StatusLid";
    ViewBag.huidigefilter = zoekNaam;

    ViewData["sort"] = sortOrder;
    ViewData["zoekNaam"] = zoekNaam;

    //Enkel de personen tonen die geen lid zijn
    var personen = PersoonBLL.SorteerZoeken(sortOrder, zoekNaam, page, huidigefilter);
    ViewData["PersonenCount"] = PersoonBLL.SorteerZoekenNotPaged(sortOrder, zoekNaam).ToList().Count;
    return View(personen);
}

[HttpPost]
public ActionResult Index(String a, String b)
{
    return View("Index");
}

我为HttpPost方法使用断点但它永远不会进入。

2 个答案:

答案 0 :(得分:0)

您需要设置每个name元素的input属性以匹配控制器方法中的参数名称。

所以你应该在你看来:

 <input type="text" id="txtA" name="txtA" />
 <input type="text" id="txtB" name="txtB" />

在你的控制器中:

public ActionResult Index(String txtA, String txtB)

答案 1 :(得分:0)

如果您想在页面中使用两个表单,则必须执行以下操作来结束表单:

  <%Html.EndForm(); %>

所以现在我的代码看起来像这样:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/personenbeheer.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UDL.Domain.Persoon>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PersonenbeheerContent" runat="server">

    <legend class="form-signin-heading">Personenlijst</legend>
    <% Html.BeginForm("Index", "Persoon", FormMethod.Get);%>
    <% var currentUser = HttpContext.Current.User; %>
    <%  if (currentUser.IsInRole("Beheerder"))
        { %>

    <nav>
        <ul class="nav nav-pills nav-justified" role="tablist">
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Maak persoon aan", "Create", null, new { @class="UDLbutton" })%></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Lijst Pdf", "PrintPersonen", new { sort = ViewData["sort"], zoeknaam = ViewData["zoekNaam"]}, new { target = "_blank",@class="UDLbutton"}) %></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#etiketten"> Adres etiketten</button></li>
            <li role="presentation" class="alert-info" style="border: 2px solid white; border-radius: 5px;"><%: Html.ActionLink("Export emailadressen","ExportEmails", new { sort = "Adres", zoeknaam = ViewData["zoekNaam"]}, new { target = "_blank",@class="UDLbutton"}) %></li>
        </ul>
    </nav>
    <%  } %>

    <%Html.EndForm(); %>

 <% Html.BeginForm("Index", "Persoon", FormMethod.Post);

       { %>

           <input type="text" id="txtA" name="txtA"/>

            <input type="text" id="txtB" name="txtB"/>

    <input type="submit" value="Verzenden"/>




     <%  } %>

    <%Html.EndForm(); %>