Reputation: 6378
I'm not so experienced using MVC. I'm dealing with this situation. Everything works well until call the HttpPost method where has all its members null. I don't know why is not persisting all the data on it.
And everything works well, because I can see the data in my Html page, only when the user submit the information is when happens this.
[HttpGet]
public ActionResult DoTest()
{
Worksheet w = new Worksheet(..);
return View(w);
}
[HttpPost]
public ActionResult DoTest(Worksheet worksheet)
{
return PartialView("_Problems", worksheet);
}
This is class which I'm using.
public class Worksheet
{
public Worksheet() { }
public Worksheet(string title, List<Problem> problems)
{
this.Title = title;
this.Problems = problems;
}
public Worksheet(IEnumerable<Problem> problems, WorksheetMetadata metadata, ProblemRepositoryHistory history)
{
this.Metadata = metadata;
this.Problems = problems.ToList();
this.History = history;
}
public string Title { get; set; }
public List<Problem> Problems { get; set; } // Problem is an abstract class
public WorksheetMetadata Metadata { get; set; }
public ProblemRepositoryHistory History { get; set; }
}
And my razor view.... the razor view shows successfully my view. I realized something rare, please note in my 5 and 6 lines that I have HiddenFor method, well if I used that, when calls HTTPPOST persists the data, I don't know why.
@model Contoso.ExercisesLibrary.Core.Worksheet
<div id="problemList">
<h2>@Html.DisplayFor(model => model.Metadata.ExerciseName)</h2>
@Html.HiddenFor(model => model.Metadata.ExerciseName)
@Html.HiddenFor(model => model.Metadata.ObjectiveFullName)
@for (int i = 0; i < Model.Problems.Count; i++)
{
<div>
@Html.Partial(Contoso.ExercisesLibrary.ExerciseMap.GetProblemView(Model.Problems[i]), Model.Problems[i])
</div>
}
</div>
UPDATE I'm using a static class to get the view name, but as I'm testing I'm just using this Partial view
@model Contoso.ExercisesLibrary.AbsoluteArithmetic.Problem1
<div>
<span style="padding:3px; font-size:18px;">@Model.Number1</span>
<span style="padding:5px; font-size:18px;">+</span>
<span style="padding:5px; font-size:18px;">@Model.Number2</span>
<span style="padding:5px; font-size:18px;">=</span>
<span style="font-size:18px">
@Html.EditorFor(model => model.Result, new { style = "width:60px; font-size:18px;" })
@Html.ValidationMessageFor(model => model.Result)
</span>
</div>
@section Scripts {
}
And here the user do the post
@model Contoso.ExercisesLibrary.Core.Worksheet
<form method="post">
@Html.Partial("_Problems", Model)
<input type="submit" value="Continue" />
</form>
Upvotes: 2
Views: 6671
Reputation: 2587
Put your entire HTML code under:
@using(Html.BeginForm())
tag.
Upvotes: 0
Reputation: 7872
You must specify a form with correct attribute in your view to perform post action
<form action="Test/DoTest" method="post">
...
</form>
or
@using(Html.BeginForm("DoTest", "Test", FormMethod.Post)) {
...
}
The second is recommended.
Upvotes: 0
Reputation: 1900
Make sure your form tag looks like the following, for instance the controller name, action method, the form method and an id for the form. I am referring to the @using statement. In my case the controller name is RunLogEntry, the action method is Create and the id is form.
Normal Post from View to Controller
@using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { id = "form", enctype = "multipart/form-data" }))
{
<div id="main">
@Html.Partial("_RunLogEntryPartialView", Model)
</div>
}
If you want to post via Jquery, could do the following:
$.post("/RunLogEntry/LogFileConfirmation",
$("#form").serialize(),
function (data) {
//this is the success event
//do anything here you like
}, "html");
Upvotes: 0
Reputation: 1571
You can use 'TempData'. It is used to pass data from current request to subsequent request means incase of redirection.
This link also helps you.
Upvotes: 1
Reputation: 4701
The Model Binder will 'bind' or link input
fields on your view to the model. It will not bind display fields (like label), that is why you need the HiddenFor
it will add an <input type="hidden"
which will then be bound to the Model when you Post.
Upvotes: 3