-1

I try to pass some hidden data to my controller by using the hiddenFor, I know the value I want gets to the view, but after submiting the form the value stays null when it arrives in the controller. The data in EditorFor is passed correctly to the controller.

// View
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    // Some working editorFor fields. Data from these gets successfully received

    // The name is correctly displayed in the paragraph
    <p>@Model.name</p>
    // This data is not received in the controller
    @Html.HiddenFor(x => x.name)

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}

// Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product, HttpPostedFileBase image)
{
    product.name = "a name";
    return View(product);
}

I also tried using a normal named hidden, but this also didn't return a value.

Someone an idea what I missed?

2
  • Yes, I have a form opened with the html helper, with a submit button inside, I updated the view in the example. I have also used hiddenfor's in other pages where they are working fine. Commented Jul 20, 2018 at 13:37
  • Never Ever use Product real class entity , Always use DTO and Viewmodel catch data from UI Commented Jul 21, 2018 at 7:14

2 Answers 2

2

You can pass the hidden fields automatically, if you have a form, using for example the razor helper

@using (Html.BeginForm("CreateTable", "Home", FormMethod.Post, null){ @HiddenFor(i => i.PropertyName) }

and the hidden fields must be inside of form, otherwise you will "lost" them.

Update following your updated question: Try remove the HiddenField and change <p>@Model.name</p>

to

@Html.LabelFor(i => i.Name)

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

2 Comments

Thanks, but I have done that. I updated my question a little so it's more clear, sorry for that. It's a problem I only have with this page, I have used hiddenfor on other pages but I really don't know why it doesn't work on this one.
LabelFor() and DisplayFor() both work correctly. But I need to add some hidden data to the form, displaying the data works. The problem I have is that if I submit the form, I get all the data correctly from the EditorFor()'s in the controller except for the data in the hidden field. This data is always null or in case of an int 0.
0

I did focus on the incorrect thing, the problem was that I changed the model in the controller after the postback. But this only changes the model en does not changes the ModelState, which the form data uses.

//This is updated after model changes.
 <p>@Model.name</p>

//For this you need to update the ModelState
@Html.HiddenFor(x => x.name)

In the controller you need to use ModelState.Remove(property name). (Or clear the complete ModelState)

//After removal of the property the ModelState will update to the new model value.
product.name = "a name";
ModelState.Remove("name");
return View(product);

In this article it's explained, https://weblog.west-wind.com/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes.

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.