Asp.net mvc 4 ASP.NET MVC-对象引用未设置为DropDownList中对象的实例

Asp.net mvc 4 ASP.NET MVC-对象引用未设置为DropDownList中对象的实例,asp.net-mvc-4,Asp.net Mvc 4,我有一个模型班 public partial class FEES { public FEES() { } public long FEE_ID { get; set; } public decimal AMOUNT { get; set; } public int CURRENCY_ID { get; set; } public string NAME { get; set; } public virtual CURRE

我有一个模型班

public partial class FEES
{
    public FEES()
    {

    }
    public long FEE_ID { get; set; }

    public decimal AMOUNT { get; set; }

    public int CURRENCY_ID { get; set; }

    public string NAME { get; set; }

    public virtual CURRENCIES CURRENCIES { get; set; }
}
视图模型

public class FeesViewModel
{
    public SelectList CurrenciesList { get; set; }
    public FeesViewModelInput input { get; set; }

    public class FeesViewModelInput
    {
        [HiddenInput]
        public long FEE_ID { get; set; }

        [Display(Name = "Amount")]
        [Required(ErrorMessage = "Fee Amount Is Required!")]
        [RegularExpression(@"^[0-9,.]+$", ErrorMessage = "Please enter proper currency format e.g. 2,500")]
        public decimal AMOUNT { get; set; }


        [Display(Name = "Currency")]
        [Required(ErrorMessage = "Currency Is Required!")]
        public int CURRENCY_ID { get; set; }

        [Required(ErrorMessage = "Fee Name Is Required!")]
        [Display(Name = "Fee Name")]
        public string NAME { get; set; }
    }
}
    public void createFees(FEES fee, FeesViewModel viewModel)
    {
        fee.FEE_ID = viewModel.input.FEE_ID;
        fee.CURRENCY_ID = viewModel.input.CURRENCY_ID;
        fee.NAME = viewModel.input.NAME.Trim();
    }
ViewModel的小型服务

    public void createFees(FEES fee, FeesViewModel viewModel)
    {
        fee.FEE_ID = viewModel.input.FEE_ID;
        fee.CURRENCY_ID = viewModel.input.CURRENCY_ID;
        fee.NAME = viewModel.input.NAME.Trim();
    }
我调用控制器中的服务和ViewModel

控制器

    public ActionResult Create()
    {
        FeesViewModel fees = new FeesViewModel();
        fees.CurrenciesList = new SelectList(_currenciesService.GetCurrencies().Where(c => c.ACTION_STATUS != 2), "CURRENCY_ID", "CURRENCY_NAME");
        fees.FeeTypesList = new SelectList(_feetypesService.GetFeeTypes().Where(c => c.ACTION_STATUS != 2), "FEE_TYPE_ID", "FEE_TYPE_NAME");
        return View();
    }


    [HttpPost]
    public ActionResult Create(FeesViewModel fees)
    {
        try
        {
            if (ModelState.IsValid)
            {
                //check if values is duplicate
                if (_feesService.GetFees().Where(c => c.ACTION_STATUS != 2).Any(c => c.NAME.ToLower().Trim() == fees.input.NAME.ToLower().Trim()))
                {
                    this.AddNotification("Fee Name already exist.<br/> Kindly verify the data.", NotificationType.ERROR);
                }
                else
                {
                    var fee = new BPP.CCSP.Admin.Web.BPPCCSPAdminFeesService.FEES();
                    var helper = new FeesService();
                    helper.createFees(fee, fees);
                    _feesService.AddFee(fee);
                    var notif = new UINotificationViewModel()
                    {
                        notif_message = "Record saved successfully",
                        notif_type = NotificationType.SUCCESS,
                    };
                    TempData["notif"] = notif;
                    return RedirectToAction("Index");

                }
            }
        }
        catch (Exception e)
        {
            this.AddNotification("Fees cannot be added.<br/> Kindly verify the data.", NotificationType.ERROR);
        }
        fees.CurrenciesList = new SelectList(_currenciesService.GetCurrencies().Where(c => c.ACTION_STATUS != 2), "CURRENCY_ID", "CURRENCY_NAME");
        return View(fees);
    }
public ActionResult Create()
{
FeesViewModel费用=新的FeesViewModel();
fees.currenceslist=new SelectList(_currencesservice.getcurrences()。其中(c=>c.ACTION_STATUS!=2),“CURRENCY_ID”,“CURRENCY_NAME”);
fees.feetypes列表=新建选择列表(\u feetypes服务.GetFeeTypes()。其中(c=>c.ACTION\u STATUS!=2),“FEE\u TYPE\u ID”,“FEE\u TYPE\u NAME”);
返回视图();
}
[HttpPost]
公共行动结果创建(FeesViewModel费用)
{
尝试
{
if(ModelState.IsValid)
{
//检查值是否重复
if(_feesService.GetFees().Where(c=>c.ACTION\u STATUS!=2).Any(c=>c.NAME.ToLower().Trim()==fees.input.NAME.ToLower().Trim())
{
此.AddNotification(“费用名称已存在。
请验证数据。”,NotificationType.ERROR); } 其他的 { var fee=new BPP.CCSP.Admin.Web.BPPCCSPAdminFeesService.FEES(); var helper=newfeesservice(); 助手。创建费用(费用,费用); _费服务附加费(费); var notif=新的UINotificationViewModel() { notif_message=“记录保存成功”, notif_type=NotificationType.SUCCESS, }; TempData[“notif”]=notif; 返回操作(“索引”); } } } 捕获(例外e) { 此.AddNotification(“无法添加费用。
请验证数据。”,NotificationType.ERROR); } fees.currenceslist=new SelectList(_currencesservice.getcurrences()。其中(c=>c.ACTION_STATUS!=2),“CURRENCY_ID”,“CURRENCY_NAME”); 返回视图(费用); }
那景色呢

@model BPP.CCSP.Admin.Web.ViewModels.FeesViewModel

@{
//ViewBag.Title = "Create";
}
<div class=" box box-body box-primary">

    @using (Html.BeginForm("Create", "Fees", FormMethod.Post, new { @class = "form-horizontal", @enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    @Html.ValidationSummary(true, null, new { @class = "text-danger" })
    @*@Html.HiddenFor(model => model.faculty_activation_date, new { @Value = System.DateTime.Now })*@
    <div class="row .col">
        <div style="margin-top:20px" class="mainbox col-md-12 col-md-offset-0 col-sm-8 col-sm-offset-2">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <div class="panel-title">Create Fee</div>
                </div>
                <div class="panel-body">

                    <div class="col-md-6">
                        <div>
                            @Html.LabelFor(model => model.input.NAME, "Fee Name")
                            @Html.TextBoxFor(model => model.input.NAME, new { @style = "border-radius:3px;", @type = "text", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.input.NAME), @autocomplete = "on" })
                            @Html.ValidationMessageFor(model => model.input.NAME, null, new { @class = "text-danger" })
                        </div>
                        <div>
                            @Html.LabelFor(model => model.input.AMOUNT, "Amount")
                            @Html.TextBoxFor(model => model.input.AMOUNT, new { @style = "border-radius:3px;", @type = "text", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.input.AMOUNT), @autocomplete = "on" })
                            @Html.ValidationMessageFor(model => model.input.AMOUNT, null, new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div>
                            @Html.LabelFor(model => model.input.CURRENCY_ID, "Currency")
                            @*@Html.DropDownList("CURRENCY_ID", (IEnumerable<SelectListItem>)ViewBag.name, "Please Select a Currency", new { @class = "form-control", @style = "border-radius:3px;" })*@
                            @Html.DropDownListFor(x => x.input.CURRENCY_ID, Model.CurrenciesList, "Please Select a Currency", new { @class = "form-control", @style = "border-radius:3px;" })
                            @Html.ValidationMessageFor(model => model.input.CURRENCY_ID, null, new { @class = "text-danger" })
                        </div>
                        <div>
                            @Html.LabelFor(model => model.input.FEE_TYPE_ID, "Fee Type")
                            @Html.DropDownListFor(model => model.input.FEE_TYPE_ID, Model.FeeTypesList, "Please Select a Fee Type", new { @class = "form-control", @style = "border-radius:3px;" })
                            @Html.ValidationMessageFor(model => model.input.FEE_TYPE_ID, null, new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                    <div class="panel-footer">
                        <div class="panel-title">
                            <div class="form-actions no-color">
                                <input type="submit" value="Create" class="btn btn-success" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</div>
}

</div>
}
@model BPP.CCSP.Admin.Web.ViewModels.FeesViewModel
@{
//ViewBag.Title=“创建”;
}
@使用(Html.BeginForm(“Create”、“Fees”、FormMethod.Post、new{@class=“form horizontal”、@enctype=“multipart/form data”}))
{
@Html.AntiForgeryToken()
@ValidationSummary(true,null,new{@class=“text danger”})
@*@Html.HiddenFor(model=>model.faculty\u activation\u date,new{@Value=System.DateTime.Now})*@
创建费
@Html.LabelFor(model=>model.input.NAME,“费用名称”)
@Html.TextBoxFor(model=>model.input.NAME,新{@style=“border radius:3px;”,@type=“text”,@class=“form control”,@placeholder=Html.DisplayNameFor(m=>m.input.NAME),@autocomplete=“on”})
@Html.ValidationMessageFor(model=>model.input.NAME,null,新{@class=“text danger”})
@Html.LabelFor(model=>model.input.AMOUNT,“AMOUNT”)
@Html.TextBoxFor(model=>model.input.AMOUNT,新的{@style=“border radius:3px;”,@type=“text”,@class=“form control”,@placeholder=Html.DisplayNameFor(m=>m.input.AMOUNT),@autocomplete=“on”})
@Html.ValidationMessageFor(model=>model.input.AMOUNT,null,new{@class=“text danger”})
@LabelFor(model=>model.input.CURRENCY\u ID,“CURRENCY”)
@*@Html.DropDownList(“CURRENCY_ID”,(IEnumerable)ViewBag.name,“请选择一种货币”,新建{@class=“form control”,@style=“border radius:3px;”)*@
@Html.DropDownListFor(x=>x.input.CURRENCY_ID,Model.currenceslist,“请选择一种货币”,新建{@class=“form control”,@style=“border radius:3px;”)
@Html.ValidationMessageFor(model=>model.input.CURRENCY_ID,null,new{@class=“text danger”})
@LabelFor(model=>model.input.FEE\u TYPE\u ID,“费用类型”)
@Html.DropDownListFor(model=>model.input.FEE_TYPE_ID,model.FeeTypesList,“请选择一种费用类型”,新建{@class=“form control”,@style=“border radius:3px;”)
@Html.ValidationMessageFor(model=>model.input.FEE_TYPE_ID,null,new{@class=“text danger”})
}
}
当我点击视图(创建)时,我得到了这个错误

CurrencyID是来自Currences模型类的下拉列表。 我有以下问题:

  • 为什么会出现此错误,以及如何解决它
  • 如何在没有映射的情况下创建ViewModel
  • 为什么会出现此错误,以及如何解决它
  • 因为视图中未设置
    模型。它是空的

    当用户访问
    Create
    页面时,您需要确保在下拉列表中为他们提供选项。因此,您需要确保在GET期间将模型传递到视图中

    public ActionResult Create()
    {
        // your code and pass fees to your view.
        return View(fees);
    }
    
  • 如何在没有映射的情况下创建ViewModel。请举个例子
  • 可以使用NuGet包进行映射

  • 为什么会出现此错误,以及如何解决它
  • 因为您没有设置
    型号