【发布时间】:2013-01-10 06:15:56
【问题描述】:
我对使用 MVC 的经验并不丰富。我正在处理这种情况。一切正常,直到调用所有成员为空的 HttpPost 方法。我不知道为什么不保留所有数据。
一切正常,因为我可以在我的 Html 页面中看到数据,只有当用户提交信息时才会发生这种情况。
[HttpGet]
public ActionResult DoTest()
{
Worksheet w = new Worksheet(..);
return View(w);
}
[HttpPost]
public ActionResult DoTest(Worksheet worksheet)
{
return PartialView("_Problems", worksheet);
}
这是我正在使用的类。
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; }
}
我的剃刀视图....剃刀视图成功显示了我的视图。我意识到了一些罕见的事情,请注意在我的第 5 行和第 6 行中我有 HiddenFor 方法,如果我使用它,当调用 HTTPPOST 时会保留数据,我不知道为什么。
@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>
更新 我正在使用静态类来获取视图名称,但在测试时我只是在使用这个部分视图
@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 {
}
用户在这里发帖
@model Contoso.ExercisesLibrary.Core.Worksheet
<form method="post">
@Html.Partial("_Problems", Model)
<input type="submit" value="Continue" />
</form>
【问题讨论】:
-
咳咳……我认为 ASP.NET MVC 的一大优点是:没有更多的回发 ...
-
你应该分享你的部分观点
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
另外,您没有显示您的代码(在视图中)在哪里调用您的 HttpPost 操作方法
-
您遇到了一些问题。您的主要问题是 MVC 不能绑定到抽象类。所以“问题”总会有问题(双关语)