2

I have a model like this

public class Roles
{
     [Key]
     public string RoleId {get;set;}
     public string RoleName {get;set;}
}

The challenge I have with this is creating a single view for the List and create. Each attempt I have made ended up in data type error. What do I do?

5
  • 1
    why are you making viewmodels for simple views , use viewmodels for some complex operation only...just make one model for your view Commented Aug 2, 2014 at 7:00
  • Lol. All that I did in my confused state of mind Commented Aug 2, 2014 at 7:16
  • But how do I prevent RoleId from being displayed in my view while I'm using ` @Html.EditorForModel()` ? Commented Aug 2, 2014 at 7:40
  • @Peter..i think my answer is elaborate enough to make you understand..??? Commented Aug 2, 2014 at 8:17
  • if you need more help then comment... Commented Aug 2, 2014 at 8:20

2 Answers 2

2

Answer 1:

In Order to hide some fields(as asked in Comments section) in @Html.EditorForModel() you have to use :

[ScaffoldColumn(false)] attribute

Ex :-

 [Key]
 [ScaffoldColumn(false)]
 public string RoleId {get;set;}

Answer 2:

and creating and showing list on the same view :

Model :

public class Roles
{
 [Key]
 public string RoleId {get;set;}
 public string RoleName {get;set;}
 public List<Roles> Roleslist { get;set; }  //Take a list in Model as shown
}

View :

 <div id="mainwrapper">

 @using (Html.BeginForm("// Action Name //", "// Controller Name //", FormMethod.Post, new { @id = "form1" }))
  {  

   @Html.TextBoxFor(m => m.RoleName)
   <input type="submit" value="Save"/>

  }
  <div id="RolesList">
   @if(Model!=null)
    {
      if(Model.Roleslist!=null)
      {
       foreach(var item in Model.Roleslist) //Just Populate Roleslist with values on controller side
       {
       //here item will have your values of RoleId and RoleName 
       }
      }
    }
  </div>

  </div>
Sign up to request clarification or add additional context in comments.

5 Comments

Good one from you! Thanks.
Error System.NullReferenceException: Object reference not set to an instance of an object on foreach(var item in Model.Roleslist) I thought I could handle it. What do you think ?
yes this error will come there should be something in Model.Roleslist to display
Even when I wrapped it within if (Model.Roleslist != null ){ ?
just see my updated answer...just give it one more wrap as shown above
2

Also check your controller to ensure you are not passing Roles.ToList() to the CreateRole view. You can do this:

  [HttpGet]
    public ActionResult CreateRole()
    {
        var roles = from ur in db.roles
                    orderby ur.RoleName
                    select ur;
        ViewBag.Listroles = roles.ToList();
        return View();
    }

where db is your DbContext.

And your view should look like this:

  @{

      if(ViewBag.Listroles != null){
      foreach (var roles in ViewBag.Listroles)
        {
            <tr>

                <td>@roles.RoleName</td>
                <td>
                      @Html.ActionLink("Edit", "EditRoles", new { id=roles.RoleId }) |
                      @Html.ActionLink("Details", "Details", new { id=roles.RoleId }) |
                      @Html.ActionLink("Delete", "Delete", new { id=roles.RoleId })
                 </td>
            </tr>
        }

      }

    }

Let me know if my answer helps.

Comments

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.