0

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

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

the beginform method you suggested does not work I have already tried this.
@wafeeq, Ok. You can use Hidden field, see my update.
0

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

1 Comment

I want to pass data from view to action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.