How to pass value from view to controller

I Have a question.

I need to pass a value in C# and MVC3 to a controller but I don’t know how.

The code in my view is :

@html.textbox("Name"); 
<input value="Envoyer" type="submit">

How can I get the value of name to my controller, please ?

Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

there is another way.

[HttpPost]
public ActionResult Index() {

    string name = Request["name"];
}

Method 2

Wrap it in a form and have a submit button of some sort to call your Action Method.

<% using (Html.BeginForm("MethodName", "Home", FormMethod.Post)) {%>
     <% Html.Textbox("Name") %> 
     <input value="Envoyer" type="submit" />
<% } %>



[HttpPost]
public ActionResult MethodName(FormCollection col)
{
    string name = col["Name"];
}

Method 3


<%= Model %>

In MVC 3, you see the <dynamic> set as model by default in your view. Just pass a string as object to the view (return View((object)”Name”);) from your controller.

Also see this for a more complex example.

[Edit]
I have got to start reading better 🙂

Ok, here we go.

The easyest way to do this, is via a <form>. Any input element is posted to your controller where the ‘name’ attribute will be the variable name (parameter).

Example:

<form action="/Contact/SendMessage" method="post">
    <table>
        <tr>
            <th>Your e-mail adres:</th>
            <td class="inputCell">
                <input id="txtEmail" name="Email" type="text" value="<%= Model %>" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea name="Message" rows="10" style="width: 450px;"></textarea>
            </td>
        </tr>
        <tr>
            <th colspan="2" style="text-align: right;"><input type="submit" id="SendButton" value="Send Message" /></th>
        </tr>
    </table>
</form>

On the controller, you can have your function like:

[AcceptVerbs(HttpVerbs.Post)]
public RedirectResult SendMessage(String Email, String Message)
{
}

Method 4

Just wrap it in a form, as has already been offered:

@using(Html.BeginForm()) {
    @Html.Textbox("Name")

    <input value="Envoyer" type="submit">
}

And then in your controller action simply declare a parameter with the name Name (assuming this is happening in a view called Index):

[HttpPost]
public ActionResult Index(string Name)
{
    // do whatever with Name
}

Using the FormCollection as has been said is also a perfectly valid option, but this is even easier and a little cleaner, in my opinion.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x