1

I am new with MVC I am trying to create a small product form with TextBox and DropDownList field, I have created a Class in Model with personName, productName. I have only 5 Products so I don't want to use db for it.

I tried all that I can by doing google but still not able to get my dropdown list

Thanks

2 Answers 2

1

Assuming you have a PersonsProduct class to represent your model/viewmodel like this

public class PersonsProduct
{

    public string PersonName { set;get;}
    public IEnumerable<SelectListItem> Products { get; set; }
    public string SelectedProductId { get; set; }
    //Other Properties
}

In your GET action method, you may initialize the values like this

public ActionResult AddProduct()
{
    var objProduct = new PersonsProduct();
    objProduct.Products = new[]
    {
          new SelectListItem { Value = "1", Text = "Book" },
          new SelectListItem { Value = "2", Text = "Pen" },
          new SelectListItem { Value = "3", Text = "Computer" },
          new SelectListItem { Value = "4", Text = "Table" },
          new SelectListItem { Value = "5", Text = "Mouse" }
    };
    // can replace the above line with loading data from Data access layer.
    return View(objProduct);
}

And in your View which is strongly typed to PersonsProduct viewmodel

@model PersonsProduct 
@using (Html.BeginForm())
{ 
  @Html.DropDownListFor(x => x.SelectedProductId, new SelectList(Model.Products ,"Value","Text"), "Select..")
  <input type="submit" value="save" />
}

And in your HTTPPost action, you can get the selected productId like this

[HttpPost]
public ActionResult Index(PersonsProduct model)
{
 //  check model.SelectedProductId here

}

NEVER HARDCODE LIKE THIS. It is UGLY UGLY UGLY !

This answers your question (without using Database and XML). But I strongly recommend to avoid this hardcoding approach. You should better use different source for the list of products like database / XML file etc.. Otherwise whenever you want to add a new product to the list, you need a recompilation. I hope you will change this code to read Product data from a different source instead of hard coded values.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but I am getting this error on postback Value cannot be null. Parameter name: items
1

If you don't want to use a database you could create a XML document that stores all of the product information.

Then you can create a Model that uses a XML parser to get all of the values from the XML document.

Once you have the values in the Model, you can bind it to the DropDownListFor<Model>.

You can also hard-code the Products into the DropDownList if they do not change.

2 Comments

is it not possible to create a dropdown without database or xml
Yes. You can hard-code them in. Do you need products to be added and removed dynamically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.