0

Always got an error and I can't really figure out what part of my code generates this error ? I tried to compare it to the other situations in the internet still can't trace what's going on. Can somebody share me your suggestions ??

Here's my View :

  @model StockroomMaitenance.Models.PG_User

      @{
        ViewBag.Title = "Create";
       }

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)


    <p>
    <a href="@Url.Action("Users", "Admin", new { id = Model.User_Id })" data-original-title="Back to List" data-toggle="tooltip">
        <i class="glyphicon glyphicon-th-list"></i>Back to List</a>
</p>
<fieldset>
    <legend>PG_User</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_Id)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.User_Id)
        @Html.ValidationMessageFor(model => model.User_Id)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_BadgeId)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.User_BadgeId)
        @Html.ValidationMessageFor(model => model.User_BadgeId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_FullName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.User_FullName)
        @Html.ValidationMessageFor(model => model.User_FullName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.User_Email)
        @Html.ValidationMessageFor(model => model.User_Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_Role, "PG_Role")
    </div>
    <div class="editor-field">
        @Html.DropDownList("User_Role", String.Empty)
        @Html.ValidationMessageFor(model => model.User_Role)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.User_Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.User_Password)
        @Html.ValidationMessageFor(model => model.User_Password)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

And heres my model:

public partial class PG_User
{
    public PG_User()
    {
        this.PG_UserAct = new HashSet<PG_UserAct>();
        this.PG_Role1 = new HashSet<PG_Role>();
    }

    public int User_Id { get; set; }
    public string User_BadgeId { get; set; }
    public string User_FullName { get; set; }
    public Nullable<int> User_Role { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password)]
    public string User_Password { get; set; }
    public string User_Email { get; set; }

    public virtual PG_Role PG_Role { get; set; }
    public virtual ICollection<PG_UserAct> PG_UserAct { get; set; }
    public virtual ICollection<PG_Role> PG_Role1 { get; set; }
  }  

And here is my controller

    public ActionResult Users(string sortUser, string searchString)
    {
        if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
        {
            return RedirectToAction("Error", "Login");
        }

        else
        {
            ViewBag.NameSort = String.IsNullOrEmpty(sortUser) ? "User_FullName" : "";
            ViewBag.BadgeSort = sortUser == "User_BadgeId" ? "user_desc" : "User_BadgeId";

            var pg_user = db.PG_User.Include(p => p.PG_Role);
            if (!String.IsNullOrEmpty(searchString))
            {
                pg_user = pg_user.Where(s => s.User_FullName.Contains(searchString));
                if (pg_user != null)
                {
                          pg_user = pg_user.Where(s => s.User_FullName.Contains(searchString));
                }
                else
                {
                    ViewBag.NotFound = "No Results Found";
                }
            }

            switch (sortUser)
            {
                case "User":
                    pg_user = pg_user.OrderByDescending(s => s.User_FullName);
                    break;
                case "User_BadgeId":
                    pg_user = pg_user.OrderBy(s => s.User_BadgeId);
                    break;
                case "user_desc":
                    pg_user = pg_user.OrderByDescending(s => s.User_BadgeId);
                    break;
                default:
                    pg_user = pg_user.OrderBy(s => s.User_FullName);
                    break;
            }

            return View(pg_user.ToList());
        }
    }




    //
    // GET: /Admin/Details/5

    public ActionResult Details(int id = 0)
    {
        if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
        {
            return RedirectToAction("Error", "Login");
        }

        else
        {
            PG_User pg_user = db.PG_User.Find(id);
            if (pg_user == null)
            {
                return HttpNotFound();
            }
            return View(pg_user);
        }
    }

    //
    // GET: /Admin/Create

    public ActionResult Create()
    {
        if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
        {
            return RedirectToAction("Error", "Login");
        }

        else
        {
            ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description");
            return View();
        }
    }
    //
    // POST: /Admin/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(PG_User pg_user)
    {
        if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
        {
            return RedirectToAction("Error", "Login");
        }

        else
        {
            if (ModelState.IsValid)
            {
                db.PG_User.Add(pg_user);
                db.SaveChanges();
                return RedirectToAction("Users");
            }

            ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description", pg_user.User_Role);
            return View(pg_user);
        }
    }



    //
    // GET: /Admin/Edit/5

    public ActionResult Edit(int id = 0)
    {
        if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
        {
            return RedirectToAction("Error", "Login");
        }

        else
        {
            PG_User pg_user = db.PG_User.Find(id);
            if (pg_user == null)
            {
                return HttpNotFound();
            }
            ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description", pg_user.User_Role);
            return View(pg_user);
        }
    }

Notices something wrong ??

2

1 Answer 1

1

For anyone with the same issue stumbling upon this question, here's the answer.

Because of the first line in your view, @model StockroomMaitenance.Models.PG_User, this is a strong typed view.

You have to pass an instance of PG_User to the view.

So in the controller action public ActionResult Create(), you should change

return View(); to

return View(new PG_User());

I am not sure why the exception is thrown on @Html.ValidationSummary, but I have tested this modification and it solves the issue (needed to remove code referencing PG_Role and PG_UserAct, first).

Sign up to request clarification or add additional context in comments.

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.