Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone, I'm just a newbie and I have a problem to my DropDownList:(MVC, ASP.NET)

Scenario:
> In View, when the dropdown is selected it display the value (Category Name)
> In Controller, instead of (category name) it will recognize only the assigned ID to the specific value

Problem:
> when it click the button save, the return value from dropdownlist is NULL
> then in the CATEGORY table it create another row with an EMPTY content
Model: (Supplier)
public class Supplier
   {
       public int Id { get; set; }
       public string SupplierCode { get; set; }
       public string SupplierName { get; set; }
       public int SupplierContact { get; set; }
       public Category Category { get; set; }
   }


Model: (Category)
public class Category
    {
        public int Id { get; set; }
        public string CatName { get; set; }
    }


Controller (Supplier)
public ActionResult New()
      {
          var CategoryMenu = _SBC.Categorys.ToList();
          var NewContent = new SupplierandCategoryViewModel()
          {
             CategoryModel = CategoryMenu,
          };
          return View(NewContent);
      }

public ActionResult Save(SupplierandCategoryViewModel Supply)
        {   
               
            var DSupply = new Supplier()
            {
                SupplierName = Supply.SupplierModel.SupplierName,
                SupplierCode = Supply.SupplierModel.SupplierCode,
                SupplierContact = Supply.SupplierModel.SupplierContact,
                Category = Supply.CategoryModel //this part is error; it cannot 
                recognize
            };

            _SBC.Suppliers.Add(DSupply);
            _SBC.SaveChanges();
            return View();
        }


View: (Supplier)
@model ShoeInformation.ViewModel.SupplierandCategoryViewModel
@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<br />
<br />
<h2>Create New Customer</h2>
<br />
<br />
@using (Html.BeginForm("Save", "Supplier"))
{
    <div class="form-group">
        @Html.LabelFor(x=>x.CategoryModel)
        @Html.DropDownListFor(x => x.CategoryModel, new SelectList(Model.CategoryModel,"Id","CatName"), "Select Supplier Category", new {id="myCat",@class="form-control" })
    </div>
    
     <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierCode)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierCode, new { @class="form-control"})
    </div>
    <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierName)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierName, new { @class="form-control"})
    </div>
    <div class="form-group">
        @Html.LabelFor(x=>x.SupplierModel.SupplierContact)
        @Html.TextBoxFor(x => x.SupplierModel.SupplierContact, new { @class="form-control"})
    </div>
    
}


ViewModel: (
SupplierandCategoryViewModel
)
public class SupplierandCategoryViewModel
    {
        public IEnumerable<Category> CategoryModel { get; set; }
        public Supplier SupplierModel { get; set; }
    }


I want to save the ID of category but in view(Index) it must display the value of ID not the ID itself

Thanks for your Response!!

What I have tried:

I try to used the model of Supply but the problem it cannot list the content of category
Posted
Updated 18-Sep-18 6:35am
Comments
F-ES Sitecore 18-Sep-18 6:21am    
Only the value of the dropdown is submitted with the form, that's just how html works. If you want the associated text then you'll need to look it up from your database so do a Find or Select on your _SBC.Categorys table to get the matching record for that ID and read the value from the result.

You might also want to consider re-architecting your database so that it's only the ID you store in your Supplier table rather than the category text. That's how databases are usually designed, it helps with searching or filtering the data later on.

1 solution

You seem to be getting very confused.

SupplierandCategoryViewModel
  • CategoryModel is the list of categories;
  • SupplierModel.Category is the selected category;

View:
@Html.DropDownListFor(x => x.CategoryModel, ...

You are trying to render a drop-down list to select the entire list of categories. You should be selecting the single category instead.

Controller:
public ActionResult Save(SupplierandCategoryViewModel Supply)
{
    ...
    Category = Supply.CategoryModel

You are trying to store the list of categories in a property which can only store a single category.


Start by simplifying your view model:
C#
public class SupplierandCategoryViewModel
{
    // The properties which will be edited:
    public int Id { get; set; }
    public string SupplierCode { get; set; }
    public string SupplierName { get; set; }
    public int SupplierContact { get; set; }
    public int CategoryId { get; set; }
    
    // The data for any drop-down lists:
    public IEnumerable<Category> AllCategories { get; set; }
}
Then update your actions:
C#
public ActionResult New()
{
    var viewModel = new SupplierandCategoryViewModel
    {
        AllCategories = _SBC.Categorys.ToList(),
    };
    
    return View(viewModel);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(SupplierandCategoryViewModel viewModel)
{
    Supplier supplier;
    if (viewModel.Id == 0)
    {
        supplier = new Supplier();
        _SBC.Suppliers.Add(supplier);
    }
    else
    {
        supplier = _SBC.Suppliers.Single(s => s.Id == viewModel.Id);
    }
    
    supplier.SupplierName = viewModel.SupplierName;
    supplier.SupplierCode = viewModel.SupplierCode;
    supplier.SupplierContact = viewModel.SupplierContact;
    supplier.Category = _SBC.Categorys.Single(c => c.Id == viewModel.CategoryId);
    
    _SBC.SaveChanges();
    return View();
}
Then your form becomes:
...
@using (Html.BeginForm("Save", "Supplier"))
{
    @Html.AntiForgeryToken()
    
    <div class="form-group">
        @Html.LabelFor(x => x.CategoryId)
        @Html.DropDownListFor(x => x.CategoryId, new SelectList(Model.AllCategories, "Id", "CatName"), "Select Supplier Category", new { id="myCat", @class="form-control" })
    </div>
    
     <div class="form-group">
        @Html.LabelFor(x => x.SupplierCode)
        @Html.TextBoxFor(x => x.SupplierCode, new { @class="form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.SupplierName)
        @Html.TextBoxFor(x => x.SupplierName, new { @class="form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.SupplierContact)
        @Html.TextBoxFor(x => x.SupplierContact, new { @class="form-control" })
    </div>
    
    <p><button type="submit" class="btn btn-primary">Save</button></p>
}

You'll probably want to add some validation attributes to your view model:
Validation with the Data Annotation Validators (C#) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900