1

这个问题的后续:如果我想根据用户从下拉列表中的选择计算一个值,并将该值放入表单变量/模型属性中,我该怎么做?

4

2 回答 2

5

真的,如果我有一个建议可以给任何 ASP.NET MVC 开发人员,那就是:使用视图模型并忘记 ViewBag/ViewData。在 99% 的情况下,这是他的问题/问题的解决方案。

因此,这是允许您正确表示下拉列表的最小视图模型:

public class MyViewModel
{
    // a scalar property on the view model to hold the selected value
    [DisplayName("item")]
    [Required]
    public string ItemId { get; set; }

    // a collection to represent the list of available options
    // in the drop down
    public IEnumerable<SelectListItem> Items { get; set; }

    ... and some other properties that your view might require
}

然后有一个控制器操作,它将填充此视图模型并将其传递给视图:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        // TODO: those values probably come from your database or something
        Items = new[]
        {
            new SelectListItem { Value = "1", Text = "item 1" },
            new SelectListItem { Value = "2", Text = "item 2" },
            new SelectListItem { Value = "3", Text = "item 3" },
        }
    };
    return View(model);
}

那么你可以有一个对应于这个视图模型的强类型视图,它可以包含一个表单和下拉列表:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.ItemId)
    @Html.DropDownListFor(x => x.ItemId, Model.Items, "--Select One--")
    <button type="submit">OK</button>
}

最后,您可以在控制器上执行相应的操作,该表单将被提交到该操作,并且您可以在其中从下拉列表中检索选定的值:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // model.ItemId will contain the selected value from the dropdown list
    ...
}
于 2012-04-23T19:05:36.300 回答
0

我猜(来自上一个问题的信息)您想根据下拉列表中的选定项目使用 ajax 获取项目价格数据,并将其作为正常表单帖子的一部分发送到您的操作方法。

Step1)为产品创建 ViewModel。

public class ProductViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
    public decimal ItemPrice { set; get; }
}

Step2)像这样创建一个产品控制器

public class ProductController : Controller
{
    public ActionResult Index()
    {
        var objProduct = new ProductViewModel();
        objProduct.Items = new[]
        {
            new SelectListItem { Value = "1", Text = "Perfume" },
            new SelectListItem { Value = "3", Text = "Shoe" },
            new SelectListItem { Value = "3", Text = "Shirt" }
        };
        return View(objProduct);
    }
    [HttpPost]
    public ActionResult Index(ProductViewModel objProduct)
    {
        //Validate and Save to DB and do whatever            
        return View(objProduct);
    }
    public string GetPrice(int itemId)
    {
        decimal itemPrice = 0.0M;
        //using the Id, get the price of the product from your data layer and set that to itemPrice variable.
        itemPrice = 23.57M;
        return itemPrice.ToString();
    }
}

步骤 3)添加强类型视图

@model MvcApplication1.Models.ProductViewModel
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

@using (Html.BeginForm())
{    
    @Html.DropDownListFor(x => x.SelectedItemId, Model.Items, "Select..")
    <div id="divItemPrice"> </div>
     @Html.HiddenFor(x=>x.ItemPrice)       
    <button type="submit">Save</button>
}
<script type="text/javascript">
$(function(){
  $("#SelectedItemId").change(function(){
    var val=$(this).val();  
    console.debug(val);    
    $.get("@Url.Action("GetPrice","Product")",  { itemId : val },function(data){
      $("#ItemPrice").val(data);
      $("#divItemPrice").html(data);
    });     
  });
});
</script>

当您更改下拉列表中的选定项目时,使用 jQuery ajax,它将调用 GetPrice Action 方法并获取数据。在 div 中显示并设置为 ItemPrice 的 HiddenField 的值。

在此处输入图像描述 当您发布表单时,您将在发布的 ViewModel 中显示它。 在此处输入图像描述

希望这可以帮助。

于 2012-04-23T19:52:25.277 回答