Neo
Neo

Reputation: 16239

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

I have two tables Customer and Status

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

<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

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

CustomerController.cs

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

Model Classes MyIdClass.cs

public class MyIdClass
    {

        private Guid myIDField;


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

    }

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

 public partial class Status : MyIdClass {
         private string nameField;

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

 }

Upvotes: 1

Views: 782

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125237

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:

@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:

@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.

Note

Using a ViewModel that contains int StatusId is a more clean way as mentioned by Stephen Muecke too in comments.

Upvotes: 2

Related Questions