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>