MVC - 更新模型和下拉列表
我正在做 MVC,并在下拉列表中查找值。调用 UpdateModel 时,仅更新查找之前的值,之后不更新任何值。但我没有收到任何错误。
我可以在我的控制器中编辑、创建和使用以下代码:
ViewData["SiteMaintenanceId"] = from m in this._siteRepository.FindAllSiteMaintenances().ToList()
select new SelectListItem
{
Text = m.Maintenance,
Value = m.SiteMaintenanceId.ToString(),
Selected = (m.SiteMaintenanceId == site.SiteMaintenanceId)
};
return View(new SiteFormViewModel(site,
this._siteRepository.FindAllSiteOperators()));
我的观点如下:
<%= Html.DropDownList("SiteOperatorId")%>
这似乎绑定正常,并允许我在编辑下拉菜单时获取所选值并创建作品。
这是我第一次使用 MVC,所以非常感谢任何帮助。
I am doing MVC and have look-up values in drop-down lists. When calling UpdateModel only values before the look-ups get updated and nothing after. I get no errors though.
I can edit and create and use the following code in my controller:
ViewData["SiteMaintenanceId"] = from m in this._siteRepository.FindAllSiteMaintenances().ToList()
select new SelectListItem
{
Text = m.Maintenance,
Value = m.SiteMaintenanceId.ToString(),
Selected = (m.SiteMaintenanceId == site.SiteMaintenanceId)
};
return View(new SiteFormViewModel(site,
this._siteRepository.FindAllSiteOperators()));
I have the following in my view:
<%= Html.DropDownList("SiteOperatorId")%>
This seems to bind ok and allows me to get the selected value on the edit of my drop down and create works.
This is my very first time doing MVC so any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来这个问题的回应很少,所以我会尝试一下。
从文本中理解问题/要求有点困难,但如果我理解正确的话,您正在尝试从下拉列表中返回一个值,对吧?如果没有,请告诉我,我会对其进行编辑以使其更适合。
然而,假设我是正确的;
为了设置我的下拉列表,我做了一些不同的事情。我认为这并不重要,但我想无论如何我都会分享它。
我有一个像这样的 FormViewModel ;
在我的 ActionResult 中,我提供以下状态;
然后我只需将其返回到视图即可。
视图有一个像这样的下拉菜单;
现在我有了状态列表。在回发时我想获取选定的状态。所以...
现在这对我有用,CalendarItem 对象中的所有字段都已填充。
但是,如果您没有获得值,您可能想尝试;
再次,我不确定这是否能回答您的问题,如果不能,请在这个答案中附加评论,我会相应地进行调整。
It seems there is very little response to this query so I'll try my hand at it.
From the text it's a little difficult to understand the problem/requirement but if I understand you correctly you are trying to get back a value from a dropdown right? If not let me know and I'll edit this for a better fit.
Assuming I am correct however;
To setup my dropdown list I do things a little different. I don't think it's important but thought I'd share it anyway.
I have a FormViewModel like this;
The in my ActionResult I have the following to provide the States;
I then simply return that to the view.
The view has a drop down like this;
So now I have my list of states. On a postback I want to get the selected state. So...
Now this works for me and all the fields within the CalendarItem object are filled in.
However if you are not getting your values you may want to try;
Again, I'm unsure if this answers your query and if it doesn't please attach a comment to this answer and I'll adjust accordingly.