DropDownList为什么它获取密钥而不是其他

发布于 2024-11-11 18:35:05 字数 593 浏览 0 评论 0原文

我的模型中有一个非常简单的属性:

在此处输入图像描述

现在这个下拉菜单无法正常工作,

@Html.DropDownListFor(m => m.Camp, new SelectList(ViewBag.Camps, "Id", "Name"))

它会返回 < code>null 而不是选择的 Camp,但如果我将其更改为:

@Html.DropDownListFor(m => m.Camp.Id, new SelectList(ViewBag.Camps, "Id", "Name"))

它将返回一个具有正确 IdCamp 对象,但 名称仍然是

为什么?

UPD:

现在另一个问题是,如果我选择第二种方法,它会搞砸不引人注目的验证。不过我可以根据所选的 ID 找到正确的营地。

I have a property in my model very simple one:

enter image description here

Now this dropDown doesn't work right

@Html.DropDownListFor(m => m.Camp, new SelectList(ViewBag.Camps, "Id", "Name"))

it returns null instead of a chosen Camp, but if I change that into:

@Html.DropDownListFor(m => m.Camp.Id, new SelectList(ViewBag.Camps, "Id", "Name"))

It would return me a Camp object with correct Id, but the Name would be still null.

Why?

UPD:

And now another problem is if I choose the second approach it would screw up with unobtrusive validation. Although I'll be able to get the right camp based on the chosen id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半衬遮猫 2024-11-18 18:35:05

这很正常。仅将 Id 发布到控制器操作。这就是表单内下拉菜单的工作原理。这就是您所能希望达到的目标。然后,您将使用此 Id 从数据库中获取相应的 Camp 对象:

[HttpPost]
public ActionResult Foo([Bind(Prefix = "Camp")]int id)
{
    Camp camp = Repository.GetCamp(id);
    ...
}

另外请删除此 ViewBag 并使用真实的视图模型:

public class CampViewModel
{
    public int Id { get; set; }
    public IEnumerable<SelectListItem> Camps { get; set; }
}

并在控制器中:

public ActionResult Index()
{
    var model = new CampViewModel
    {
        Camps = Repository.GetCamps().Select(x => new SelectListItem
        {
            Value = x.Id.ToString(),
            Text = x.Name
        })
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(int id)
{
    Camp camp = Repository.GetCamp(id);
    ...
}

和观点:

@model CampViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(
        x => x.Id,
        new SelectList(Model.Camps, "Value", "Text")
    )
    <input type="submit" value="OK" />
}

That's normal. Only the Id is posted to the controller action. That's how dropdown inside forms work. So that's all you can hope to get there. You will then use this Id to get the corresponding Camp object from the database:

[HttpPost]
public ActionResult Foo([Bind(Prefix = "Camp")]int id)
{
    Camp camp = Repository.GetCamp(id);
    ...
}

Also please get rid of this ViewBag and use a real view model:

public class CampViewModel
{
    public int Id { get; set; }
    public IEnumerable<SelectListItem> Camps { get; set; }
}

and in the controller:

public ActionResult Index()
{
    var model = new CampViewModel
    {
        Camps = Repository.GetCamps().Select(x => new SelectListItem
        {
            Value = x.Id.ToString(),
            Text = x.Name
        })
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(int id)
{
    Camp camp = Repository.GetCamp(id);
    ...
}

and the view:

@model CampViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(
        x => x.Id,
        new SelectList(Model.Camps, "Value", "Text")
    )
    <input type="submit" value="OK" />
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文