简体   繁体   中英

How to pass values by POST in ASP.NET MVC 4

I have problem with passing values by POST in ASP.NET MVC 4 This is my action in User controller:

[HttpPost]
public string Show(int? uid, string uname)
{
    return uname + uid.ToString();
}

And this is how I tried to pass values in view:

@using (Html.BeginForm("Show", "User"))
{
    Html.Hidden("uid", Model.Id);
    Html.Hidden("uname", Model.UserName);
    <input type="submit" value="+"/>
}

html:

 <form action="/User/Show" method="post"> <input type="submit" value="+"/> </form>

and:

@using(Html.BeginForm("Show", "User", FormMethod.Post, new { uid = 1, uname = "user1" }))
{
    <input type="submit" value="+"/>
}

html:

<form action="/User/Show" method="post" uid="1" uname="user1"> <input type="submit" value="+"/></form>

In both ways Show action receives null instead real values.

Your HtmlHelpers are not being rendered. Use Razor syntax.

@using (Html.BeginForm("Show", "User"))
{
    @Html.Hidden("uid", Model.Id);
    @Html.Hidden("uname", Model.UserName);
    <input type="submit" value="+"/>
}

Explanation:

Calling Html.Hidden (or Html.[anything] ) is a method and usually returns an IHtmlString . Without using @ infront, the engine doesn't know that you're trying to output the returned string. It just thinks you're calling a method.

This is not a good approach for an action that receives data . This approach can offer many security breaches, like data injection. , essentially lots of fields.

The right thing is create a Model (or a ViewModel, if you don't want to persist the data) to make the correct guidance between View and Controller:

ViewModel:

public class MyViewModel {
    public int? uid { get; set; }
    public string uname { get; set; }
}

View:

@model MyProject.ViewModels.MyViewModel

@using (Html.BeginForm("Show", "User"))
{
    Html.HiddenFor(model => model.uid);
    Html.HiddenFor(model => model.uname);
    <input type="submit" value="+"/>
}

Controller:

public ActionResult Show(int modelId) {
    var model = context.Model.SingleOrDefault(m => m.ModelId == modelId);
    var viewModel = new MyViewModel();

    viewModel.uid = model.Id;
    viewModel.uname = model.UserName;

    return View(viewModel);
}

[HttpPost]
public string Show(MyViewModel viewModel)
{
    return viewMode.uname + viewModel.uid.ToString();
}

You're not actually creating hidden fields in your form. You need the @ in front of you Hidden helper, and drop the semi-colon at the end.

@using (Html.BeginForm("Show", "User"))
{
    @Html.Hidden("uid", Model.Id)
    @Html.Hidden("uname", Model.UserName)
    <input type="submit" value="+"/>
}

The reason your hard-coded test didn't work is that your HTML wasn't correct. You can't just put the values you want to post on the <form> element, you need to have them as hidden fields in the form.

<form action="/User/Show" method="post">
    <input type="hidden" name="uid" value="1" />
    <input type="hidden" name="uname" value="user1">
    <input type="submit" value="+"/>
</form>

It would be better if you could use a view model.

public class MyViewModel 
{
   public int? Id {get;set;}
   public string UserName {get;set;}
}

public ActionResult Show()
{
    return View(new MyViewModel());
}

[HttpPost]
public ActionResult Show(MyViewModel model)
{
    return Content(string.format("{0} - {1}", model.Id, model.UserName));
}

(Coded in notepad so untested, but should work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM