简体   繁体   中英

How to get parameters of action of controller from view post

I have a controller action in asp.net mvc application

 public ActionResult ForFahrzeug(DateTime? initDate = null, DateTime? finalDate = null, long id = 0, long days = 0, string ExpGnr = "")
    {
        //body
    }

I am calling this above method in a view as below

 @using (Html.BeginForm("ForFahrzeug", "Messungen", FormMethod.Get))
{     

      <P>Fahrten von:
      @Html.TextBox("initDate", "", new { style = "width:15%", @class = "datefield" })  bis: 
      @Html.TextBox("finalDate", "", new { style = "width:15%", @class = "datefield" }) 

     <input type="submit" value="Filter" /> </p> 
} 

I want to get the value of "ExpGnr" from view. How could I do it? I tried to do it like below but it does not work.

@using (Html.BeginForm("ForFahrzeug", "Messungen", FormMethod.Get, new { ExpGnr = "Smart_ED"}))
{     
     Fahrten von:
      @Html.TextBox("initDate", "", new { style = "width:15%", @class = "datefield" })  bis: 
      @Html.TextBox("finalDate", "", new { style = "width:15%", @class = "datefield" }) 

     <input type="submit" value="Filter" /> </p> 
} 

I think i understand your problem. Seems like you choose incorrect overload of method @Html.BeginForm(). You should write something like that:

@using (Html.BeginForm("ForFahrzeug", "Messungen", new { ExpGnr = "Smart_ED"},FormMethod.Get))

Always choose correct overload of MVC extensions OR use VisualStudio help messages for it.

And look,is any important reason why you submit your form through GET method? In most cases form should be submitted using POST request.

And, i suppose you need use some ViewModel for you Form. ViewModel-based Solution is be more clear, extensible and traditionally for MVC). You also can add ExpGnr property in ViewModel and pass it to controller through @Html.HiddenFor(x => x.ExpGnr) Also, this topic will be useful for you.

Update

Ok. this will work:

@using (Html.BeginForm("ForFahrzeug", "Messungen", FormMethod.Get))
{     
      @Html.Hidden("ExpGnr","Smart_ED")
      @Html.TextBox("initDate", "", new { style = "width:15%", @class = "datefield" })
      @Html.TextBox("finalDate", "", new { style = "width:15%", @class = "datefield" }) 
     <input type="submit" value="Filter" /> 
}

Thank you)

If I understand correctly you wanna pass some data from action to view. You should use ViewBag for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM