1408786user
1408786user

Reputation: 2014

Send parameter to action method

At the moment I have this code:

@using (Html.BeginForm("Add", "Review", "Review"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Review</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.TEKST)
        </div>
         <div class="editor-field">
            @Html.TextBoxFor(model => model.TEKST)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

This will send me to: localhost:4470/Review/Add?Length=6 The thing that I actually want is this url: localhost:4470/Review/Add?tekst=sdfsdf

How can I modify this code that it will use "tekst" as parameter instead of length? And as value the content of the textbox.

UPDATE: This are my action methods:

public ActionResult Create()
{
    return View();
}

public ActionResult Add(string tekst)
{
    ViewBag.test = tekst;
    return View();
}

In the View page of Create, I would like to have a form, with a textbox or textarea, that sends an action to Add, the content of the textbox or textarea should be in parameter "tekst" of action method "Add"

SOLUTION: See the post of CD Smith

Upvotes: 2

Views: 375

Answers (2)

CD Smith
CD Smith

Reputation: 6607

I'm not sure what your action looks like but as long as it is accepting your model, you have the value for TEKST in your model..

If you need something different then you're View would need to be different as well, you post a Model from the view, you don't send GET parameters by making a POST

Do you have an action that currently takes TEKST as a parameter?

UPDATE

Ok, looking at your action... you don't need to modify your view, you need to modify your actions, try this this will get you what you want.

Change YourModelTypeHere to match your real model type

[HttpPost]
public ActionResult Create(YourModelTypeHere model)
{
    return RedirectToAction("Add", new { tekst = model.tekst });
}

public ActionResult Add(string tekst) 
{
    ViewBag.test = tekst;
    return View();
}

You need to modify your view just a tad - remove the parameters from the BeginForm tag

@using (Html.BeginForm())

So the Create action will render the Create View, then POST back to the Create method that has the [HttpPost] annotation. Then the value of TEKST will be sent as a parameter to the Add method as a RedirectToAction and render the Add view

Upvotes: 1

gerrod
gerrod

Reputation: 6637

I'm not sure where the "Length=6" is coming from, as I can't see what part of your code would create that URL...

It sounds to me that you're expecting a GET request, but (by default) a form will result in a POST request. Based on your code, I would expect the generated route to be "localhost:4470/Review/Add". Whatever the contents of the form are will be sent as form data.

So in your controller, if your action method looked like this:

[HttpPost]
[ActionName("Add")]
public ActionResult AddViaPost(string tekst)
{
    // do something with tekst
}

Then you should get the value of the text box posted correctly to your controller.

Upvotes: 1

Related Questions