简体   繁体   English

无法从控制器MVC中的下拉列表中获取选定的外键值

[英]Unable to get a selected foreign key value from dropdown in controller mvc

I have two tables Customer and Status 我有两个表CustomerStatus

CustomerController.cs CustomerController.cs

// GET: Customers/Create
        public ActionResult Create()
        {
            P  var Query = from d in db.Status
                        orderby d.Name
                                   select d;
            ViewBag.Status = new SelectList(Query, "myStatusId", "Name", selected);
            return View();
        }

Create.cshtml Create.cshtml

<div class="editor-field">
            @Html.DropDownListFor(model => model.status, ViewBag.Status as SelectList, String.Empty)
            @Html.ValidationMessageFor(model => model.status)
        </div>

Now I'm able to get all Status in Customer/Create.cshtml page My Issue is when I select any status from dropdown and click on create i dont get any status value in my customer object it is NULL 现在,我可以在Customer/Create.cshtml页面中获取所有状态。我的问题是,当我从下拉列表中选择任何状态并单击创建时,我的客户对象中没有任何状态值,它为NULL

having following exception {"The parameter conversion from type 'System.String' to type 'Status' failed because no type converter can convert between these types."} 具有以下异常{"The parameter conversion from type 'System.String' to type 'Status' failed because no type converter can convert between these types."}

CustomerController.cs CustomerController.cs

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {

Model Classes MyIdClass.cs 模型类MyIdClass.cs

public class MyIdClass
    {

        private Guid myIDField;


        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public Guid myID
        {
            get { return myIDField; }
            set { myIDField = value; }
        }

    }

Customer.cs Customer.cs

public partial class Customer : MyIdClass
    {
        public string Name {get; set;}

        private Status statusField;

          public Status status
        {
            get
            {
                return this.statusField;
            }
            set
            {
                this.statusField = value;
            }
        }
}

Status .cs 状态.cs

 public partial class Status : MyIdClass {
         private string nameField;

  public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }

 }

Supposing you have a StatusId field in your Customer , If you except StatusId you need to call Html.DropDownListFor for the ForeignKey column property of your Customer instead of the whole ForeignKey object property: 假设你有一个StatusId场在你的Customer ,如果你只是StatusId你需要调用Html.DropDownListForForeignKey您的列属性Customer ,而不是整个ForeignKey对象属性:

@Html.DropDownListFor(model => model.StatusId, ViewBag.Status as SelectList, String.Empty)

But if you don't have such property and you excpect Status , then you can use it this way: 但是,如果您没有此类属性并且期望使用Status ,则可以通过以下方式使用它:

@Html.DropDownListFor(model => model.status.myStatusId, ViewBag.Status as SelectList, String.Empty)

@*/
If you need the value of model.status.Name 
You can set it using a simple script on the dropdown change, 
otherwise, you don't need to use a hiddenfor
*@
@Html.HiddenFor(model => model.status.Name)

Then in a Post it should initialize your customer.Status property with values. 然后在Post应使用值初始化您的customer.Status属性。

Note 注意

Using a ViewModel that contains int StatusId is a more clean way as mentioned by Stephen Muecke too in comments. 正如Stephen Muecke在评论中提到的那样,使用包含int StatusIdViewModel是一种更干净的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM