MVC - 更新模型和下拉列表

发布于 2024-08-02 04:42:45 字数 661 浏览 4 评论 0原文

我正在做 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2024-08-09 04:42:45

看来这个问题的回应很少,所以我会尝试一下。

从文本中理解问题/要求有点困难,但如果我理解正确的话,您正在尝试从下拉列表中返回一个值,对吧?如果没有,请告诉我,我会对其进行编辑以使其更适合。

然而,假设我是正确的;

为了设置我的下拉列表,我做了一些不同的事情。我认为这并不重要,但我想无论如何我都会分享它。

我有一个像这样的 FormViewModel ;

public class CalendarEventFormViewModel
{
    public CalendarItem Event { get; set; }
    public SelectList States;
}

在我的 ActionResult 中,我提供以下状态;

fvm.States = new SelectList(Enumerations.EnumToList<Enumerations.AustralianStates>(), "Value", "Key", fvm.Event.state);

然后我只需将其返回到视图即可。

视图有一个像这样的下拉菜单;

<% using (Html.BeginForm()) { %>
  <%=Html.DropDownList("selectedState", Model.States, new { @class="stateSelector" })%>
<%} %>

现在我有了状态列表。在回发时我想获取选定的状态。所以...

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false), Authorize]
public ActionResult Add(FormCollection collection)
{
    CalendarItem fvm = new CalendarItem();
    UpdateModel(fvm);
}

现在这对我有用,CalendarItem 对象中的所有字段都已填充。

但是,如果您没有获得值,您可能想尝试;

String state = collection["selectedState"];

再次,我不确定这是否能回答您的问题,如果不能,请在这个答案中附加评论,我会相应地进行调整。

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;

public class CalendarEventFormViewModel
{
    public CalendarItem Event { get; set; }
    public SelectList States;
}

The in my ActionResult I have the following to provide the States;

fvm.States = new SelectList(Enumerations.EnumToList<Enumerations.AustralianStates>(), "Value", "Key", fvm.Event.state);

I then simply return that to the view.

The view has a drop down like this;

<% using (Html.BeginForm()) { %>
  <%=Html.DropDownList("selectedState", Model.States, new { @class="stateSelector" })%>
<%} %>

So now I have my list of states. On a postback I want to get the selected state. So...

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false), Authorize]
public ActionResult Add(FormCollection collection)
{
    CalendarItem fvm = new CalendarItem();
    UpdateModel(fvm);
}

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;

String state = collection["selectedState"];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文