Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need to pass the stored procedure result (column named 'username') to Home page view. Here, loginid and password as provided by user is getting passed in stored procedure
AM1_Login
.

var Res = db.AM1_Login(mod.loginid, mod.password).ToList();
if (Res.Count > 0)
{
TempData["Usr"] = Res.ToList();
return RedirectToAction("Home", "Login");

}


Var Res contains below:

C#
-		Res	Count = 1	System.Collections.Generic.List<string>
		[0]	"UserName"	string
+		Raw View		


What I have tried:

I have tried using TempData as it is best in case of redirection. But unable to get the proc result on home page.
Posted
Updated 28-Sep-16 4:05am
Comments
Karthik_Mahalingam 28-Sep-16 0:41am    
does the TempData["Usr"] contains data?
and how you are accessing tempdata in cshtml
Member 11114915 - Tanvi 28-Sep-16 0:58am    
Hey.. TempData["Usr"] contains data which is same as of 'Res'
Richard Deeming 28-Sep-16 10:18am    
"Here, loginid and password as provided by user is getting passed in stored procedure"

Sounds like you're storing passwords in plain text. Don't do that. You should only ever store a salted hash of the password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Member 11114915 - Tanvi 28-Sep-16 14:50pm    
thanks Richard...will surely try to implement the same....:)

1 solution

There are plenty of ways of passing data from a controller to a view. What is not clear is if you are trying to submit data from a view to the controller, then redirect to a different view and still maintain that same data so I'll give you some options.

1) Showing data from your controller into your view

C#
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new IndexModel();
        model.Username = "dwimbley";
        return View(model);
    }
}


In your view you'd have

HTML
@model IndexModel

@Model.Username


Which would display dwimbley

This is considered a strongly typed view. I prefer this over other options as I think handling everything via viewbag or tempdata creates a nightmare if you have other devs on your team.

2) Lets say you've submitted a form to an action in a controller. Thant from that POST action you want to display something onto a different view, which i think might be what you are asking.


C#
public class HomeController : Controller 
{
   public ActionResult Index()
   {
        TempData["username"] = "dwimbley";
        return RedirectToAction("About");
   }

   public ActionResult About()
   {
       return View();
   }
}


Since index action redirects to about. The only view that matters is About.cshtml. Also, unlike the above example, you don't need a model since you are using tempdata.

HTML
@TempData["username"]


One suggestion here. If you are trying to store an object as tempdata and then casting it back to the object type it should be in your view, you are breaking the principles of MVC. I highly recommend you use the strongly typed view route and only utilize TempData/ViewBag/ViewData as exceptions for items that don't fit into your views model.
 
Share this answer
 
Comments
Member 11114915 - Tanvi 28-Sep-16 14:49pm    
thanks David...i hv sorted this in different manner..by using elementat()...thank you for the solution....:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900