0
@{
    var items = new List<SelectListItem>(){
        new SelectListItem {Text = "One", Value = "1"},
        new SelectListItem {Text = "Two", Value = "2"},
        new SelectListItem {Text = "Three", Value = "3"},
        new SelectListItem {Text = "Four", Value = "4"},
        new SelectListItem {Text = "Five", Value = "5"}
    };
 }
@Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "Value", "Text", Model.InvoiceItem.Count), new { style = "width:95%" })

Model.InvoiceItem.Count 是整数,在控制器中设置为 5。

加载视图时,我在下拉列表中看到“五”作为默认值,但是当我回发到控制器时,模型属性 Invoice.Count 始终为“0”。任何人都可以建议我缺少什么。提前致谢。

Here is my controller with add and save action methods
        public ActionResult Add() {
            if (PassportSession.GetPassportProfile() == null)
                return PartialView("~/Areas/Ship/Views/Ship/_Redirect.cshtml");
            var invoiceModel = (InvoiceModel)Session["InvoiceModel"];
            invoiceModel.InvoiceItem = new InvoiceItem();
            invoiceModel.InvoiceItem.OriginCountry = "US";
            invoiceModel.InvoiceItem.Count = 5;
            invoiceModel.AddEdit = true;
            invoiceModel.Add = true;
            invoiceModel.InvoiceItem.Line = invoiceModel.InvoiceItems.Count + 1;
            invoiceModel.InvoiceItems.Add(invoiceModel.InvoiceItem);
            invoiceModel.InvoiceItem.Unit = "EA ";
            Session["InvoiceModel"] = invoiceModel;

            return PartialView("_Footer", invoiceModel);
        }

public ActionResult Save(InvoiceModel returnModel) {
            if (PassportSession.GetPassportProfile() == null)
              return PartialView("~/Areas/Ship/Views/Ship/_Redirect.cshtml");
            //if (string.IsNullOrEmpty(returnModel.InvoiceItem.PartNumber))
            //    ModelState.AddModelError("InvoiceItem.PartNumber", "Part Number is required.");
            if (string.IsNullOrEmpty(returnModel.InvoiceItem.OriginCountry))
                ModelState.AddModelError("InvoiceItem.OriginCountry", "Origin Country is Required.");
            if (string.IsNullOrEmpty(returnModel.InvoiceItem.Description))
                ModelState.AddModelError("InvoiceItem.Description", "Part Description is Required.");
            if (!string.IsNullOrEmpty(returnModel.InvoiceItem.HarmCode) && ((returnModel.InvoiceItem.HarmCode.Length < 6) || (returnModel.InvoiceItem.HarmCode.Length > 15)))
                ModelState.AddModelError("InvoiceItem.HarmCode", "Tariff is 6-15 chars.");
            if (returnModel.InvoiceItem.Price<=0)
                ModelState.AddModelError("InvoiceItem.Price", "Price need to be greater than Zero.");


            var invoiceModel = (InvoiceModel)Session["InvoiceModel"];
            if (ModelState.IsValid) {
                invoiceModel.InvoiceItem = new InvoiceItem();
                invoiceModel.AddEdit = false;

                var invoiceItem = invoiceModel.InvoiceItems.Where(x => x.Line == returnModel.InvoiceItem.Line).FirstOrDefault();
                if (invoiceItem == null && returnModel.InvoiceItem.Line>0) //this is for Covance Change Request
                {
                    invoiceItem = new InvoiceItem();
                    invoiceModel.InvoiceItems.Add(invoiceItem);
                }
                invoiceItem.Count = returnModel.InvoiceItem.Count;
                invoiceItem.CreateDate = DateTime.Now;
                invoiceItem.Custom01 = returnModel.InvoiceItem.Custom01;
                invoiceItem.Custom02 = returnModel.InvoiceItem.Custom02;
                invoiceItem.Custom03 = returnModel.InvoiceItem.Custom03;
                invoiceItem.Custom04 = returnModel.InvoiceItem.Custom04;
                invoiceItem.Custom05 = returnModel.InvoiceItem.Custom05;
                invoiceItem.Custom06 = returnModel.InvoiceItem.Custom06;
                invoiceItem.Custom07 = returnModel.InvoiceItem.Custom07;
                invoiceItem.Custom08 = returnModel.InvoiceItem.Custom08;
                invoiceItem.Custom09 = returnModel.InvoiceItem.Custom09;
                invoiceItem.Custom10 = returnModel.InvoiceItem.Custom10;
                invoiceItem.Description = returnModel.InvoiceItem.Description;
                invoiceItem.HarmCode = returnModel.InvoiceItem.HarmCode;
                invoiceItem.Line = returnModel.InvoiceItem.Line;
                invoiceItem.OriginCountry = returnModel.InvoiceItem.OriginCountry;
                invoiceItem.PartNumber = string.IsNullOrEmpty(returnModel.InvoiceItem.PartNumber) ? string.Empty : returnModel.InvoiceItem.PartNumber;
                invoiceItem.Price = returnModel.InvoiceItem.Price;
                //invoiceItem.ShipId = returnModel.InvoiceItem.ShipId;
                invoiceItem.Unit = returnModel.InvoiceItem.Unit;

                Session["InvoiceModel"] = invoiceModel;
            }
            return PartialView("_Footer", invoiceModel);
        }

在添加操作方法中,我将 count 属性设置为 5 并将其传递给视图,但是当我将表单发回保存方法时,它为 0。

4

0 回答 0