Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC中从ListBox解析动作中的'null'值_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 如何在MVC中从ListBox解析动作中的'null'值

C# 如何在MVC中从ListBox解析动作中的'null'值,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,萨拉蒙·阿勒库姆 我正在从MVC中的列表框中获取null值 HR\u TP\u供应商空分配1MedicinemastPharmacy.Models.HR\u TP\u供应商 下面是CSHTML代码 <div class="form-group"> @Html.LabelFor(model => model.HR_TP_Supplier, "Supplier", htmlAttributes: new { @class = "control-label c

萨拉蒙·阿勒库姆

我正在从MVC中的列表框中获取
null

HR\u TP\u供应商空分配1MedicinemastPharmacy.Models.HR\u TP\u供应商

下面是CSHTML代码

<div class="form-group">
            @Html.LabelFor(model => model.HR_TP_Supplier, "Supplier", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">

                @Html.ListBox("HR_TP_Supplier", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.HR_TP_Supplier, "", new { @class = "text-danger" })
            </div>
        </div>
这是实体类

namespace Assignment1MedicineMasterPharmacy.Models
{
    using System;
    using System.Collections.Generic;

    public partial class PH_tmedicine_masterSubmit
    {
        public int MedicineID { get; set; }
        public string Med_Name { get; set; }
        public string Med_code { get; set; }
        public Nullable<int> Generic_Name { get; set; }
        public Nullable<int> Trade_Price { get; set; }
        public Nullable<int> Retail_Price { get; set; }
        public Nullable<int> Accounting_Unit { get; set; }
        public Nullable<int> Nature { get; set; }
        public Nullable<int> THERAPEUTIC_Group { get; set; }
        public string Dosage { get; set; }
        public string Description { get; set; }
        public string Active { get; set; }
        public string Enteredby { get; set; }
        public Nullable<System.DateTime> Enteredon { get; set; }
        public Nullable<int> ClientID { get; set; }
        public virtual HR_TP_Supplier HR_TP_Supplier { get; set; }
    }
}
命名空间分配1MedicineMasterPharmacy.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类医学博士
{
public int MedicineID{get;set;}
公共字符串名称{get;set;}
公共字符串Med_代码{get;set;}
公共可空泛型_名称{get;set;}
公共可为空的交易价格{get;set;}
公共可空零售价格{get;set;}
公共可空记帐单元{get;set;}
公共可空性质{get;set;}
公共可空组{get;set;}
公共字符串剂量{get;set;}
公共字符串说明{get;set;}
公共字符串活动{get;set;}
{get;set;}输入的公共字符串
公共可为null的Enteredon{get;set;}
公共可为空的ClientID{get;set;}
公共虚拟人力资源供应商人力资源供应商{get;set;}
}
}
我应该如何解析它我想要列表框中的多个选定值,如列表、数组或字符串


谢谢

在视图中使用ORM创建的实体并不是一个好主意,尤其是当您的视图不止一个或两个字段时。 您应该为视图创建视图模型

您的
PH\u tmedicine\u masterSubmit
实体上的
HR\u TP\u Supplier
属性不是集合类型。不确定为什么要使用多选列表框。无论如何,如果确实希望视图中的多选列表框,可以添加字符串数组属性来存储多选列表框中的选定项

public class MedicineMasterVm
{      
  public string[] SelectedSuppliers { get; set; }  
  public IEnumerable<SelectListItem> Suppliers { get; set; }    

  //Add Other needed properties here
  public string Med_Name { get; set; }
  public string Med_code { get; set; }
}
现在在您的视图中,强类型为
MedicineMasterVm

@model UserViewModel
<h2>Create Medicine </h2>
@using (Html.BeginForm())
{
  <label>Name</label> @Html.TextBoxFor(s=>s.Med_Name)
  <label>Code</label> @Html.TextBoxFor(s=>s.Med_Code)
  <label>Suppliers</label> @Html.ListBoxFor(s => s.SelectedSuppliers ,Model.Suppliers)
  <input type="submit" value="Save" />
}
如果需要一个单选普通下拉列表,请将字符串数组更改为仅一个字符串或int,并使用中所述的字符串或int

public ActionResult Create()
{
    var vm = new MedicineMasterVm();
    //The below code is hardcoded for demo. you mat replace with DB data.
    vm.Suppliers= new[]
    {
      new SelectListItem { Value = "1", Text = "Supplier 1" },
      new SelectListItem { Value = "2", Text = "Supplier 2" },
      new SelectListItem { Value = "3", Text = "Supplier 3" }
    };  
    return View(vm);
}
@model UserViewModel
<h2>Create Medicine </h2>
@using (Html.BeginForm())
{
  <label>Name</label> @Html.TextBoxFor(s=>s.Med_Name)
  <label>Code</label> @Html.TextBoxFor(s=>s.Med_Code)
  <label>Suppliers</label> @Html.ListBoxFor(s => s.SelectedSuppliers ,Model.Suppliers)
  <input type="submit" value="Save" />
}
[HttpPost]
public ActionResult CreateUser(MedicineMasterVm model)
{
    if (ModelState.IsValid)
    {
        string[] supplierArray= model.SelectedSuppliers  ;
        //check items now
        //do your further things and follow PRG pattern as needed
    }
    //reload the Suppliers property again in the ViewModel before returning to the View
    return View(model);
}