ASP.NET MVC 编辑问题 - “实体”来自控制器的数据未传递到视图
我很确定我的问题变得复杂了。抱歉,我不知道如何表达自己。接下来的情况是:
public ActionResult Edit(int id)
{
CreateTrainingModel editTrainingModel = new CreateTrainingModel();
editTrainingModel.Training = training.GetByID(id);
editTrainingModel.Player = player.GetAll();
return View(editTrainingModel);
}
Html.EditorFor() 的编辑器模板:
@inherits System.Web.Mvc.WebViewPage<TrainingStatistics.Models.ViewModels.CreateTrainingModel>
@Html.ValidationSummary()
<table>
@Html.HiddenFor(x => x.Training.TrainingID)
<tr>
<td>Training Date</td>
<td><input type="text" id="datepicker" name="Training.Date"></td>
<td>@Html.ValidationMessageFor(x => x.Training.Date)</td>
</tr>
</table>
问题是当我想编辑它时,模型中的数据不会传递到视图。我认为这个问题是由于使用 ViewModel 造成的,而我在这里做错了。当我尝试更新仅由一个实体(示例播放器)组成的内容时,并且当我不使用 ViewModel 时,一切都很好。
先感谢您!
I'm pretty sure that I complicated my question. Sorry, I didn't know how to express myself. Situation is next :
public ActionResult Edit(int id)
{
CreateTrainingModel editTrainingModel = new CreateTrainingModel();
editTrainingModel.Training = training.GetByID(id);
editTrainingModel.Player = player.GetAll();
return View(editTrainingModel);
}
and Editor template for Html.EditorFor() :
@inherits System.Web.Mvc.WebViewPage<TrainingStatistics.Models.ViewModels.CreateTrainingModel>
@Html.ValidationSummary()
<table>
@Html.HiddenFor(x => x.Training.TrainingID)
<tr>
<td>Training Date</td>
<td><input type="text" id="datepicker" name="Training.Date"></td>
<td>@Html.ValidationMessageFor(x => x.Training.Date)</td>
</tr>
</table>
Problem is that when I want to edit this, data from model aren't passed to the view. I think that problem is because of using ViewModels and I'm doing something wrong here..When I try to update something which is consist of only one entity (example Player) and when I'm not using ViewModel everything is fine.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要为视图模型创建编辑器模板。
你可以在下面看到这个例子
http://www.codecapers。 com/post/Display-and-Editor-Templates-in-ASPNET-MVC-2.aspx
http://davidhayden.com/blog/dave/archive/ 2009/08/21/htmleditorforscaffoldcolumnattribute.aspx
I believe you need to create editor template for the view model.
You can see the exmaples of this below
http://www.codecapers.com/post/Display-and-Editor-Templates-in-ASPNET-MVC-2.aspx
http://davidhayden.com/blog/dave/archive/2009/08/21/htmleditorforscaffoldcolumnattribute.aspx