0

I have a form in MVC:

<% using (Html.BeginForm("Get", "Person"))
  { %>
      <%= Html.TextBox("person_id")%>
      <input type="submit" value="Get Person" />
  <% } %>

This redirects me to Person/Get. Okay. The question:

How do I make this Form so it redirects me to Person/Get/{person_id}?

Edit:

<% using (Html.BeginForm("Get", "Person", new { id = ??? }))
  { %>
      <%= Html.TextBox("person_id")%>
      <input type="submit" value="Get Person" />
  <% } %>

What do I write in ???

3
  • Regarding "???"; you can't do that because id is not known at server time. Commented Oct 6, 2011 at 11:09
  • @CarlR I want to get the text from the textbox in ???. Why is it so hard to understand my question? This should be easy, but I will accept your answer if this can't be done. Commented Oct 6, 2011 at 11:17
  • The text in the textbox is entered by the user right? The closest you can get is by changeing the BeginForm to recieve a FormMethod.Get. That way what is sent to the server would be /Person/Get?person_id=234 or whatever the user enters. The other thing you could do is intercept the submit event using jquery and read the person_id from javascript and then alter the adress before submitting. I wouldn't go that way. Commented Oct 6, 2011 at 12:45

2 Answers 2

1

I think the most difficult way would be using a javascript clientside.

The more straightforward way is to retrieve it on the action Person/Get and from there return a RedirectResult pointing to Person/Get/{person_id}

[HttpPost]
public ActionResult Get(string person_id)
{
    return RedirectToAction("Get", "Person", new { id = person_id });
}

[HttpGet]
public ActionResult Get(string id)
{
     //Do your thing
}

The redirect is usually so fast that the user will never notice. He/she will arrive at /Person/Get/{person_id}

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

2 Comments

I solved my problem this way, but I needed some other name for this Get (because you can't overload just by renaming the parameter)
I thought the post and get attributes would be enough. Thanks for letting me know! :)
0

What you want to do is specify the route values as the third parameter on the BeginForm method.

<% using (Html.BeginForm("Get", "Person", **new { person_id = this.Model}**))
{ %>
   <%= Html.TextBox("person_id")%>
   <input type="submit" value="Get Person" />
<% } %>

Then your controller action would look something like this

public ActionResult Get(int person_id)
{
    return View(person_id);            
}

Comments

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.