0

I have multiple dropdownlist, i want to bind the data on page load, I'm working on MVC 4. In single stored procedure, i'm selecting data from different table. Each select for one dropdownlist. here i assigning value to list, i want to return everything in a single list to controller. herewith i have given single dropdown as a sample.

public class SampleClass1
{

     public string ID { get; set; }
    public string Cr_PId { get; set; }
    public string Cr_cId { get; set; }
    public string Res { get; set; }
    public string Descr { get; set; }
    public IList<ApplicationMaster> lstappmas { get; set; }
}

  public class ApplicationMaster
{
    public string ApplicationId { get; set; }
    public string ApplicationName { get; set; }

}
         public List<CrimsDetailModel> GetCrimsDetails()
    {
        List<CrimsDetailModel> lstcrimsdtls = new List<CrimsDetailModel>();
        List<ApplicationMaster> lstappdtls = new List<ApplicationMaster>();
        DataSet dscrmodel = new DataSet();
        Hashtable htcrmodel = new Hashtable();
        dscrmodel = DataProxy.FetchDataSet("GetCrimDetails");
        dscrmodel.Tables[0].TableName = "CRdetails";
        dscrmodel.Tables[1].TableName = "ApplicatonMaster";

        try
        {
            foreach (DataRow dr in dscrmodel.Tables["CRdetails"].Rows)
            {
                CrimsDetailModel objcrmodel = new CrimsDetailModel();
                objcrmodel.ID = dr["CR_PaId"].ToString();
                objcrmodel.Cr_PId = dr["CR_PId"].ToString();
                objcrmodel.Cr_cId = dr["CRIMS_CId"].ToString();
                objcrmodel.Res = dr["Res"].ToString();
                objcrmodel.Desc = dr["Desc"].ToString();

                lstcrimsdtls.Add(objcrmodel);
              }
           foreach (DataRow dr in dscrmodel.Tables["Appdetails"].Rows)
            {
                ApplicationMaster objapp = new ApplicationMaster();
                objapp.ApplicationId = dr["ApplicationId"].ToString();
                objapp.ApplicationName = dr["ApplicationName"].ToString();

                lstappdtls.Add(objapp);
            }
       return lstcrimsdtls;

}

When i assign

1 Answer 1

1

You are using MVC (Model-View-Controller). You should assign your data to the Model class in the Controller. You can display your Model in a View. Codeproject - Learn MVC step by step

Do I haz teh codez? For sure! :>

First, you create a ViewModel that holds the data to be displayed. It also has a getter to convert the possible options into the collection of SelectListItem required by Razor.

 public class ContainsADropdownViewModel {

    public CrimsDetailModel Selected { get; set; }

    // to be read in controller
    public IEnumerable<CrimsDetailModel> RawOptions { get; set; }

    // generate SelectListItems to be used with DropDownListFor()
    public IEnumerable<SelectListItem> Options { get { 
        foreach (var detail in RawOptions) {
            yield return new SelectListItem {
                Value = detail.ID, // something unique
                Text =  detail.Desc, // shown to User
                Selected = detail == Selected // stays selected after roundtrip
            };
        }
    }}
}

Then you create a Controller containing two Actions, one to display the page initially [HttpGet] and one to handle the postback of the form [HttpPost].

The GET action should return a strongly typed view, using the ViewModel. Here is the place to read from the database!

public class MyDropdownTestController : Controller {

    // GET MyDropdownTest/Index
    [HttpGet]
    public ActionResult Index() {
        var vm = new ContainsADropdownViewModel();
        vm.RawOptions = GetCrimsDetails(); // read from backend
        return View("Index", vm);
    }

    // POST MyDropdownTest/Index
    [HttpPost]
    public ActionResult Index(ContainsADropdownViewModel vm) {
        var optionChosenByUser = vm.Selected;
        // process form, send HTTP 200 OK or whatever
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }
}

The View "Index" displays the dropdown and submits the filled out ViewModel to the post action specified in Html.BeginForm.

// Razor View in Views\MyDropdownTest\Index.cshtml
@model ContainsADropdownViewModel 
@using (Html.BeginForm("Index", "MyDropdownTest", FormMethod.Post)) {
    @* render dropdown; bind selected value to Model.Selected *@
    @Html.DropDownListFor(m => m.Selected, Model.Options)
    <button type="submit">Submit</button>
}  
Sign up to request clarification or add additional context in comments.

4 Comments

i want lstcrimsdtls and lstappdtls in a single list
Then tune the method that generates the IEnumerable<SelectListItem> to include options from both raw lists. Just be careful that SelectListItem.Value is unique.
sorry i have miscommunication, i want a list which contains lstcrimsdtls and lstappdtls. lstappdtls used for drop down, lstcrimsdtls for the textboxes and pages.
I am not sure if I unterstand your requirements, but maybe you can split the GetCrimsDetails() method into two, one returning the lstcrimsdtls and one returning the lstappdtls. Then use only the lstappdtls as RawData for the dropdown.

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.