0

I have created a mobile store project. There is a dropdown in my view, I need to fill this dropdown from database so I have declared a public variable in controller but it's not accessible in views. I have also tried to declare that in model class, it's working fine, but problem is that when I update the entity model from database these variable got deleted. So please suggest me how to do that and also I don't want to use ViewBag. Code which I have tried.

public SelectList Vendor { get; set; }

public ActionResult ShowAllMobileDetails(tbInsertMobile IM)
{                   
     Vendor = new SelectList(db.Usp_VendorList(), "VendorId", "VendorName");
     return View(IM);
}

Views..

@model  MobileApplicationEntityFramework.Models.tbInsertMobile

@{
     ViewBag.Title = "ShowAllMobileDetails";
}

@using (@Html.BeginForm())
{    
    <h2>ShowAllMobileDetails</h2>
    <p>
        @Html.ActionLink("Create New Mobile", "InsertMobile");
    </p>
    <fieldset>
        <legend>Search Panel</legend>
        <table>
            <tr>
                <td>Mobile Name</td>
                <td>@Html.TextBoxFor(a => a.MobileName) </td>
            </tr>
            <tr>
                <td>Mobile Manufacured</td>
                <td>@Html.DropDownList("ddlVendor", Vendor, "Select Manufacurer")</td>
            </tr>            
        </table>
    </fieldset>
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Raghubar
  • 2,768
  • 1
  • 21
  • 31
  • 3
    Create a view model with property `public SelectList Vendor { get; set; }` and the other properties you want to edit (and probably also `public int `SelectedVendor { get; set; }` for bind the dropdownlist to) –  Jan 12 '15 at 06:45
  • 1
    don't you want to add these properties in `partial` class? Then you don't have problem with update your entity model. But the best practice is to have separate `ViewModel` classes or `DTO` classes for your Views. – teo van kot Jan 12 '15 at 06:50
  • @StephenMuecke Thanks for your reply. I have also thinking about that but not getting any proper example. Could you provide some example link. – Raghubar Jan 12 '15 at 06:51
  • 1
    I suggest you start with the [answers here](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) which explain what a view model is and why use it. Also [this article](http://www.mikesdotnetting.com/article/188/view-model-design-and-use-in-razor-views) –  Jan 12 '15 at 07:08
  • Done using ViewModel. Thanks for your help guys.... – Raghubar Jan 12 '15 at 07:43
  • Nothing to do with Classic ASP. – Paul Jan 13 '15 at 15:37

1 Answers1

0
hi: you can define a static property to share to all place in your project:



 public static class Global
    {

    public static SelectList Vendor()
    {
      return (new SelectList(db.Usp_VendorList(), "VendorId", "VendorName"));
    }

    }

and use in your views like this:

Mobile Manufacured @Html.DropDownList("ddlVendor", namespaceOfGlobal.Vendor, "Select Manufacurer")

HamidReza
  • 1,726
  • 20
  • 15