如何在mvc4中使用tempdata从控制器调用值来查看。 [英] How to call a value from controller to view using tempdata in mvc4.

查看:71
本文介绍了如何在mvc4中使用tempdata从控制器调用值来查看。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要将存储过程结果(名为username的列)传递给主页视图。这里,用户提供的loginid和密码在存储过程

 AM1_Login 

中传递。



var Res = db.AM1_Login(mod.loginid,mod.password).ToList();

if(Res.Count> ; 0)

{

TempData [Usr] = Res.ToList();

返回RedirectToAction(Home,Login );



}





Var Res包含以下内容:< br $>


  -  Res Count =  1  System.Collections.Generic.List<串GT; 
[ 0 ] UserName string
+ Raw View





我尝试了什么:



我尝试过使用TempData,因为它最好是重定向。但是无法在主页上获得proc结果。

解决方案

有很多方法可以将数据从控制器传递到视图。不清楚的是,如果您尝试将视图中的数据提交给控制器,然后重定向到另一个视图并仍然保留相同的数据,那么我将为您提供一些选项。



1)将控制器中的数据显示在您的视图中



  public   class  HomeController:Controller 
{
public ActionResult Index( )
{
var model = new IndexModel();
model.Username = dwimbley;
return 查看(模型);
}
}





在您看来,你有



 @ model IndexModel 

@ Model.Username





哪个会显示dwimbley



这被视为强类型视图。我更喜欢这个而不是其他选项,因为我认为如果你的团队中有其他开发人员,通过viewbag或tempdata处理一切都会造成噩梦。



2)让我们说你已经将表单提交给控制器中的操作。从那个POST动作想要在不同的视图上显示内容,我想你可能就是这样。





< pre lang =c#> public class HomeController:Controller
{
< span class =code-keyword> public ActionResult Index()
{
TempData [ username] = dwimbley;
return RedirectToAction( 关于);
}

public ActionResult关于()
{
return 查看();
}
}





由于索引操作重定向到约。重要的唯一视图是About.cshtml。此外,与上面的示例不同,您不需要模型,因为您使用的是tempdata。



 @ TempData [username ] 





这里有一个建议。如果您试图将对象存储为tempdata,然后将其强制转换为它应该在您的视图中的对象类型,那么您就违反了MVC的原则。我强烈建议您使用强类型视图路径,并仅将TempData / ViewBag / ViewData用作不适合您的视图模型的项目的例外。


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:

-		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.

解决方案

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

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



In your view you'd have

@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.


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.

@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.


这篇关于如何在mvc4中使用tempdata从控制器调用值来查看。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆