ASP.NET MVC 编辑问题 - “实体”来自控制器的数据未传递到视图

发布于 2024-09-16 07:00:57 字数 983 浏览 4 评论 0原文

我很确定我的问题变得复杂了。抱歉,我不知道如何表达自己。接下来的情况是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文