我们如何将值从控制器传递到视图,反之亦然 [英] How can we pass value from controller to view and vise versa

查看:93
本文介绍了我们如何将值从控制器传递到视图,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的登录页面,这将验证用户。



在我看来

I''m trying to create simple login page, which will validate user.

In my view

<script type="text/javascript">
    $(document).ready(function () {
        $(''#btnLogin'').click(function () {
            document.location = ''@Url.Action("Validateuser", "Login")'';
        });
        $(''body'').css("background", "#517EAD");
        $(''body'').css("color", "Green");
        $(''body'').css("margin", "0");
    });
</script>
<div style="background:white;text-align:right;padding:0px 50px;">
      <h2>Please Login</h2>
</div>

<div style="float:right;">
    <table border="0">
<tr>
    <td>
        <label id="lbllogin">LoginID:</label>        
    </td>
    <td>
            <input type="text" id="txtLogin" required placeholder="Prashant" style="border-radius:5px;background:orange;"/>
    </td>
</tr>
<tr>
    <td></td>
    <td>
        <input type="submit" id="btnLogin" value="Login" style="border-radius:15px;background:silver;"/>
    </td>
</tr>
</table>
</div>





如何将文本框值传递给控制器​​?



我在控制器上有2个动作,如下所示



How i can pass textbox value to the controller??

I have on controller with 2 actions as follows

namespace MVC.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index() // has Index.cshtml view
        {
            return View();
        }
        
        public ActionResult Validateuser() // has Validateuser.cshtml view
        {
            Login objLogin = new Login();
            int intUser = objLogin.validateUser();
            //ViewData["Msg"] = "Invalid User";
            return RedirectToAction("Index","Login");
        }
    }
}





如果您在上面的代码中检查,请在Validateuser()中没有写任何逻辑,但我正在尝试重定向到Index.cshtml视图,并希望将无效用户按摩传递到我将显示的视图。







我能说怎么做?



提前谢谢



In above code if you check, in Validateuser() i have not written any logic but i''m trying to redirect to Index.cshtml view and want to pass "Invalid User" massage to the view where i will display.



can i know how can i do this??

thanks in advance

推荐答案

(document).ready(function(){
(document).ready(function () {


(''#btnLogin'')。click(function(){
document.location =''@ Url.Action(Validateuser,Login)'';
});
(''#btnLogin'').click(function () { document.location = ''@Url.Action("Validateuser", "Login")''; });


(''body'') .css(background,#517EAD);
(''body'').css("background", "#517EAD");


这篇关于我们如何将值从控制器传递到视图,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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