1

I have to use the Database first approach, the solution has 3 projects. 2 are class libraries the third is the actual MVC project. One class library has the edmx designer. The other has Has different model classes. These 2 libraries are referenced by the MVC project. My problem is that the object is not populating values. I guess it is a naming conflict somewhere. The object tblRDBUser is not getting populated. There is a table in the database with the same object name too. The object attributes match the column names too. I am not getting the values from the view into the controller.

Project one:

RDB.DataModel, it contains the EDMX designer.

Project two: RDB.Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDB.DataModel;

public class Authenticate
{
    public string AuthenticateUser(tblRDBUser User)
    {
        Book_TradEntities users = new Book_TradEntities();
        return (from p in users.tblRDBUsers where p.UserName == User.UserName && p.Password == User.Password select p.UserName).FirstOrDefault();
    }

}

Project 3:

View:

@model RDB.DataModel.tblRDBUser

@{
    ViewBag.Title = "Index";
}
@Html.TextBoxFor(a => a.UserName)
@Html.TextBoxFor(a => a.Password)
@Html.ActionLink("Login", "Authenticate");

Controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RDB.DataModel;
using RDB.Models;

namespace CignaRDB.Controllers
{
public class RDBWebController : Controller
{
    //
    // GET: /RDBWeb/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Authenticate(tblRDBUser User)
    {
        if (ModelState.IsValid)
        {
            Authenticate user = new Authenticate();
            user.AuthenticateUser(User);
            return View();
        }
        return View();
    }
}
}
6
  • Do I really see plain-text password storage? or is this simplification of your code? Commented Oct 10, 2016 at 22:15
  • 1
    @trailmax Oh, this is simplified code. Commented Oct 10, 2016 at 22:15
  • 1
    You will have to put a bit more explanation of your problem. What object is not populated? and how do you think your layers affect this? Commented Oct 10, 2016 at 22:16
  • 1
    ah! good! Say no plain-text password storage! -)) Commented Oct 10, 2016 at 22:17
  • I think you are missing 'form' element in your view. You didn't mention which object is not populating. Is it you are not getting values from the view to the controller when you click on the button? Commented Oct 10, 2016 at 22:31

1 Answer 1

2

The main problem. that you create link @Html.ActionLink("Login", "Authenticate") which after click on him just redirect user to Authenticate method. Insted of a link you should generate button which make submit of you form. So your view code should be something like this

@model RDB.DataModel.tblRDBUser
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Authenticate"))
{
   @Html.TextBoxFor(a => a.UserName)
   @Html.TextBoxFor(a => a.Password)

   <input type="submit" value="Login" />

}

Also you controller looks strange, maybe because this is result of code simplification, but at least should be next change when your model is invalid (return View("Index");):

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RDB.DataModel;
using RDB.Models;

namespace CignaRDB.Controllers
{
public class RDBWebController : Controller
{
    //
    // GET: /RDBWeb/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Authenticate(tblRDBUser User)
    {
        if (ModelState.IsValid)
        {
            Authenticate user = new Authenticate();
            user.AuthenticateUser(User);
            return View();
        }
        return View("Index");//because view name and Action method not match
    }
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.

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.