如何在ASP.NET MVC模型保存选定的DropDownList值POST? [英] How to save selected DropDownList value in ASP.NET MVC model for POST?

查看:122
本文介绍了如何在ASP.NET MVC模型保存选定的DropDownList值POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的财产模型中,像这样的:

I have a property inside my model, like this:

 public IList<SelectListItem> AllCards { get; set; }

属性被填充 SelectListItem 通过与数据库中的数据控制器S

Property is being filled with SelectListItem's by controller with data from database.

另外,在我看来是 @ Html.DropDownListFor(M = GT; m.AllCards,Model.AllCards - 选择 - )
视图的发布过程中的问题occures。也就是说,我的onsubmit Model.AllCards 是空的,虽然里面的dropdownlist选择一个值。
我尝试添加一个新的属性到模型是这样的:

Also, in my View is @Html.DropDownListFor(m=>m.AllCards,Model.AllCards,"--Select--"). Problem occures during POSTing of a view. That is, onSubmit my Model.AllCards is empty, although there is a value selected inside dropdownlist. I tried adding a new property to the model like this:

public SelectListItem SelectedCard

,然后改变看法是这样的:

and then changing the view like this:

@Html.DropDownListFor(m=>m.AllCards,Model.SelectedCard,"--Select--")

但它给了我一个错误说转换不可能,某事这样的。

but it gives me an error saying conversion not possible, sth like that.

我应该怎么做赶上我的DropDownList值,以便它可以是内部模型一旦视图的POST版本被称为?
谢谢

What should I do to catch my dropdownlist value so that it can be inside model once POST version of a view is called? Thank you

P.S。我知道有十个类似的问题,但他们没有帮助。

P.S. I know there are ten similar questions but none of them help.

推荐答案

这是我会怎么做它。

您的视图模型可能看起来像这一点,将有卡的列表:

Your view model could look like this and will have a list of cards:

public class CardViewModel
{
     public int CardId { get; set; }

     public IEnumerable<Card> Cards { get; set; }
}

我不知道是什么性质的卡,但让我创造我自己的:

I do not know what properties your card has but let me invent my own:

public class Card
{
     public int Id { get; set; }

     public string Name { get; set; }
}

您认为会接受这个视图模型:

Your view will accept this view model:

@model YourProject.ViewModels.Cards.CardViewModel

对于卡您的HTML标记下拉列表:

Your HTML markup for the cards drop down list:

@Html.DropDownListFor(
     x => x.CardId,
     new SelectList(Model.Cards, "Id", "Name", Model.CardId),
     "-- Select --",
     new { id = "Cards" }
)
@Html.ValidationMessageFor(x => x.CardId)

您操作方法,当视图加载的第一次:

Your action method when the view loads for the first time:

public ActionResult YourActionMethod()
{
     CardViewModel viewModel = new CardViewModel
     {
          Cards = cardService.FindAll()
     }

     return View(viewModel);
}

[HttpPost]
public ActionResult YourActionMethod(CardViewModel viewModel)
{
     if (!ModelState.IsValid)
     {
          return View(viewModel);
     }

     // Do what ever you need to do. The selected card's id will now be populated

     return RedirectToAction("List");
}

关于你的JavaScript问题,我woudld建议你开始一个新的问题,但我会给你可以看出来的轮廓。通过使用的jQuery (使用任何你想)在更改事件添加独立的JavaScript和HTML。例如:

Regarding your JavaScript question I woudld advise that you start a new question, but I will give an outline on what you can look out for. Separate your JavaScript and HTML by adding the on change event using jQuery (use whatever you want to). For example:

<script>

     $(document).ready(function () {
          $("#Cards").change(function () {
               alert('cards value has changed');
          });
     });

</script>

我希望这有助于。

I hope this helps.

这篇关于如何在ASP.NET MVC模型保存选定的DropDownList值POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆