简体   繁体   中英

Issue with Drop Down list in MVC

Issue with dropdown list in MVC

New to MVC I am having issue with the Drop down list in MVC could you please help me with that

Below is the Code that I am using for getting the Data from DB

public static List<AppDetailsDTO> GetAllApplications()
{
    string[] arrRoles = new string[] { };
    List<AppDetailsDTO> lstAppDetails = new List<AppDetailsDTO>();
    using (ApplicationSecurityContext objSecurityContext = new ApplicationSecurityContext())
    {
    var appDetails = objSecurityContext.Applications.Select(app => new AppDetailsDTO() { AppId= app.id, AppName= app.name }).Distinct().ToList();
    if (appDetails.Count() > 0)
     {
        lstAppDetails = appDetails;
     }
    }
return lstAppDetails;   
}

AppDetailsDTO class looks like as below

public class AppDetailsDTO
{
    public int AppId { get; set; }
    public string AppName { get; set; }
}

ViewModel which is using in the view is as below

public class ClientApplicationViewModel
{
    public string selectedAppID;
    public SelectList AppSelectionList { get; set; }
}

Controller Code is as below

[HttpGet]
public ActionResult AssignApplication()
{
    List<AppDetailsDTO> lstAvailableApps = new List<AppDetailsDTO>();
    lstAvailableApps = SecurityDAL.GetAllApplications();
    ClientApplicationViewModel objModel = new ClientApplicationViewModel();
    objModel.AppSelectionList = new SelectList(lstAvailableApps, "AppId", "AppName");
    return View(objModel);
}

[HttpPost]
public void AssignApplication(ClientApplicationViewModel model)
{
    string id = model.selectedAppID.ToString();
}

I am using in the view as below

@model MVCWINDOWSAUTHENTICATION.Models.ClientApplicationViewModel
@{
ViewBag.Title = "AssignApplication";
}

<h2>AssignApplication</h2>

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ClientApplicationViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@Html.DropDownListFor(model => model.selectedAppID, Model.AppSelectionList, "Select Application")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

The issue that I am getting Drop Down list is getting populated but when I am posting the form it gets posted and comes in controller post method but in the controller but the selectedAppID and AppSelectionList is coming as null.

Please help me with this and let me know what is the mistake that I am making in this.

Appreciate your help in this regard.

For MVC Model Binding to work, all items that you want to automatically bind from the Form Post have to be properties :

public class ClientApplicationViewModel
{
    public string selectedAppID { get; set; }  // <-- add this
}

On a separate note, Lists such as AppSelectionList are typically not part of the Model because they should never be posted back to the Controller. The Model should only contain the value(s) that the user has chosen or entered, and not the List(s) of possible values. Such 'helper'-Lists are typically sent from the Controller to the View using a value in ViewBag .

So please remove AppSelectionList from the Model. The code in the Controller then becomes this:

ViewBag.AppSelectionList = new SelectList(lstAvailableApps, "AppId", "AppName");

And in the View use this:

@Html.DropDownListFor(model => model.selectedAppID, ViewBag.AppSelectionList, "Select Application")

Finally, if you want to populate the list in both scenarios (ie. both for GET and POST), then the ViewBag init-code should be part of both action methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM