-1

I have a SelectList which functions perfectly, unless the model is is passed back to the View again (eg when the model is invalid). When it is passed back I get the error below. What am I missing?

Line that throws an InvalidOperationException:

@Html.DropDownListFor(m => m.companyId, Model.CompanyList, "SELECT COMPANY")

I tried:

if g.companyId = null; commented out:

InvalidOperationException: The ViewData item that has the key 'companyId' is of type 'System.String' but must be of type 'IEnumerable'.

if g.companyId = null; is not commented out:

InvalidOperationException: There is no ViewData item of type 'IEnumerable' that has the key 'companyId'.

controller

[HttpPost]
public ActionResult GCLandingAddUser(GUser g)
{
    //will not accept if phone is same as email and sanitize text inputs

    if (!g.isValid()) {
        g.companyId = null; 
        return View("AddGCLUser", g);
    }
}
public ActionResult GCLandingAddUser()
{
  GUser g=new GUser();
  g.companyList=GetSelectList();
  return View("AddGCLUser",g);
}

Model

public class GUser
{
[Required(ErrorMessage = "company is required")]
[Display(Name = "company id")]
public string companyId { get; set; }

public List<SelectListItem> GetSelectList(){
  var GroupList = new List<SelectListItem>();
  GroupList.Add(new SelectListItem{Text="1",Value="1"});
  GroupList.Add(new SelectListItem{Text="2",Value="2"});
  return GroupList;
}

public bool isValid(){return false;}//guarantees it will be passed back
}

View (GCLandingAddUser.cshtml)

@model GUser
@using (@Html.BeginForm())
{
<div class="panel-body" id="MyDiv">
    <div>
        @Html.LabelFor(m => m.companyId)
        @Html.DropDownListFor(m => m.companyId, Model.CompanyList, "SELECT COMPANY")
        @Html.ValidationMessageFor(m => m.companyId)
    </div>
</div>  
<input type="submit" value="Submit" />
}  
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • Could you provide a more complete example so others can replicate the problem? Also, which line of code is throwing the error? – Jasen Jun 07 '18 at 18:57
  • @Jasen, Does that help? Let me know if you need more of the code. – Rilcon42 Jun 07 '18 at 19:17
  • Your view declares the model `GUser` and you access a `CompanyList` property that doesn't appear in your `GUser` class. Also, how are you submitting to the controller action? – Jasen Jun 07 '18 at 19:55
  • @Jasen Thanks I added the property and the form submission. – Rilcon42 Jun 07 '18 at 20:04
  • Your naming conventions for local variables and properties are against [Microsoft Guidlines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions). `GroupList` in `GetSelectedList` should be camcel case `groupList`. `companyId` should be pascal case `CompanyId` . – Erik Philips Jun 07 '18 at 20:10
  • `@Html.DropDownListFor(m => m.companyId, Model.CompanyList` is looking for `CompanyList` on the defined model. The model is `@model GUser`. `GUser` does not have a property called `CompanyList`. – Erik Philips Jun 07 '18 at 20:14

1 Answers1

0

You need to fill the DropDownList with data again, if you want to return same View to the user.

[HttpPost]
public ActionResult GCLandingAddUser(GUser g)
{    
    if (ModelState.IsValid) 
    {
        // So something      
        return RedirectToAction("Index");
    }
    g.CompanyList = GetSelectList(); // <------
    return View("AddGCLUser", g);
}
Win
  • 61,100
  • 13
  • 102
  • 181