Displaying some of the database data in .NET MVC textboxes

Viewed 58

I have to display 9 fields with labels. 6 of them are hardcoded in the database (because they should be non-editable) and the rest of 3 are user inputs. The codes are attached herewith. Please help!

My model is:

public partial class User
    {
        public int UserID { get; set; }

        [DisplayName("User Name")]
        [Required(ErrorMessage="Please enter your User Name!")]
        public string UserName { get; set; }
        
        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Please enter your Password!")]
        public string Password { get; set; }

        public string LoginErrorMessage { get; set; }

        /* My changes */
        [DisplayName("Name")]
        public string Name { get; set; }

        [DisplayName("Father's Name")]
        public string FatherName { get; set; }

        [DisplayName("CNIC (without dashes)")]
        public int CNIC { get; set; }

        [DisplayName("Address")]
        public string Address { get; set; }

        [DisplayName("Line Manager")]
        public string LineManager { get; set; }

        [DisplayName("Department")]
        public string Department { get; set; }

        [DisplayName("Designation")]
        public string Designation { get; set; }
    }
}

My controller:

namespace MVC_Login_6Dec.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            var data = db.Users.ToList();
            return View();
        }

        /* My Changes */

            private LoginDatabaseEntities db = new LoginDatabaseEntities();

            public ActionResult GetList()
            {
                var userData = (from user in db.Users select user).ToList();
                return View(userData);
            }

            [HttpPost]
            public ActionResult GetList(User model)
            {
                var data = db.Users.ToList();
                return View(data);
            }
    }
}

My View:

@model MVC_Login_6Dec.Models.User
@{
    Layout = null;
    if (Session["userID"] == null)
    {
        Response.Redirect("~/Login/Index");
    }
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div"> 
        <h1>Employee Details</h1>
        <div>
            <h4>UserName: @Session["userName"].ToString()</h4>
        </div>
        <div>
            <a href="@Url.Action("Logout","Login")">Logout</a>
        </div>
        <br>
    </div>
    <div>
    @using (Html.BeginForm("GetList", "Home", FormMethod.Get))
    {
        
        <table>
            <tr>
                <td>@Html.LabelFor(Model => Model.Name)</td>
                <td>@Html.TextBoxFor(Model => Model.Name)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.FatherName)</td>
                <td>@Html.TextBoxFor(Model => Model.FatherName)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.CNIC)</td>
                <td>@Html.TextBoxFor(Model => Model.CNIC)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.Address)</td>
                <td>@Html.TextBoxFor(Model => Model.Address)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.LineManager)</td>
                <td>@Html.TextBoxFor(Model => Model.LineManager)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.Department)</td>
                <td>@Html.TextBoxFor(Model => Model.Department)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(Model => Model.Designation)</td>
                <td>@Html.TextBoxFor(Model => Model.Designation)</td>
            </tr>
        </table>
    }
    </div>
</body>
</html>

The data is not shown in the fields and they are empty.

My Output:

Output Screenshot

1 Answers

Your screenshot of the output doesn't show the route (URL) for the page you are displaying, so I'm not sure which controller action you are calling. However, your view requires a MVC_Login_6Dec.Models.User object, and none of your controller actions are passing a single User object to the view. Let's say you have a view called "Edit", you would need an action that did something like this:

public ActionResult Edit(int UserId)
{
     var user = db.Users.Where(x => x.UserID == UserId).FirstOrDefault()
     return View(user);
}

You would then call this action by going to http://localhost/home/edit/{the id}. You also need to make sure your App_Start\RouteConfig.cs is configured in the "normal" way:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", 
            id = UrlParameter.Optional }
    );
}

If you put a debug breakpoint inside this controller action, you will also be able to inspect the user object to see if your Database Entities object is configured properly and returning data.

Related