user2915962
user2915962

Reputation: 2711

Pass value to controller from post-method

This is my form:

@using (Html.BeginForm())
{        
    <h3 class="text-blue title-article editable">thisStr</h3>

    <input type="submit" value="xxxx" class="btn btn-success" name="xxxx"/>
} 

@model.title is part of an inline-editable that i can change to whatever, what i need to do is pass the string in the h3 to this controller-method:

 public ActionResult method(string test)
        {   
            someProp = test;

            return View();
        }

I think my problem is this line:

<input type="submit" value="xxxx" class="btn btn-success" name="xxxx"/>

Can someone help me out, thanks!

Upvotes: 0

Views: 42

Answers (2)

Martin Costello
Martin Costello

Reputation: 10862

Add a @Html.HiddenFor((p) => Model.Title) inside your form, which will then submit the text back with the POST for you.

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Put this in the form:

@Html.HiddenFor(x=>x.Title)

view:

@using (Html.BeginForm("YourAction","YourController",FormMethod.Post))
{        
    <h3 class="text-blue title-article editable">@Model.Title</h3>

     @Html.HiddenFor(x=>x.Title)

    <input type="submit" value="xxxx" class="btn btn-success" name="xxxx"/>
}

in your action:

[HttpPost]
public ActionResult YourAction(FormCollection form)
        {   
            sting vl= form["Title"].ToString();

            return View();
        }

Upvotes: 1

Related Questions