MVC将数据从视图传递到视图

时间:2014-09-10 09:40:31

标签: jquery asp.net-mvc html5 visual-studio-2013

我正在尝试通过jquery将值输入到文本框中,并将其从我的登录视图传递到我的索引视图以显示用户名。我尝试使用波纹管,但没有显示任何内容。

如何做到这一点?什么是推荐的方法?

登录:

<script>
$("#loginbutton").click(function () {
    var username = ($("#username").val());
    ViewBag.Usersname = username;
});
</script>

<body>
<form action="CheckUser" method="post" class="form-horizontal col-md-6 col-md-offset-4">
<h1>Welcome to the Report Login</h1><br />

    <label class="control-label">Username or Email:</label><br />
    <input type="text" class="form-control" name="username">    <br />

    <label class="control-label">Password:</label><br />
    <input type="password" class="form-control" name="password"> <br />

    <input type="checkbox" name="remember" value="rememberme" /> Remember me <br />
    <a href="@Url.Action("ForgotPassword", "Home")" class="elements"><span>Forgot Password?</span></a><br />

    <div class="col-md-7 text-center">
        <button type="submit" class="btn btn-success" id="loginbutton">Log In</button>
    </div>
</form>
</body>

索引包含:

@ViewBag.Usersname

2 个答案:

答案 0 :(得分:1)

登录视图:-example

<script>

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}

function SetCookieFromModel() {
       user = @ViewBag.Usersname
       if (user != "" && user != null) {
           setCookie("username", user, 30);
       }
}

</script>

索引视图: -

<script>
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
function GetCookieFromModel() {
var user=getCookie("username");
}
</script>

在MVC中使用TempData的第二种方式我在这里做了一个简单的例子。在这里,我在第二个视图中获得员工ID。 根据此

模拟您的示例

型号:

  public class Information
    {

        public string EmpId { get; set; }

    }

控制器:

public class EmpController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }    
        [HttpPost]
        public ActionResult Index(Information ifn ,string info)
        {

            var getInfo = ifn.EmpId;
            TempData["EmployeeInformation"] = getInfo;

            return RedirectToAction("ShowEmployee");
        }           
        public ActionResult ShowEmployee()
        {
            return View();            
        }
    }

查看索引:

  <div>
      @using (Html.BeginForm()) {
              @Html.LabelFor(m => m.EmpId)
                @Html.TextBoxFor(m => m.EmpId)             
      }
    </div>

ShowEmployee的第二个视图:

 <div>
     your info : @TempData["EmployeeInformation"]

    </div>

答案 1 :(得分:0)

有一个简单的解决方案,你所要做的就是设置一个cookie并从控制器中读取它

(function () {
    var onLoginClick = function() {
        var userName = $('txtUserName').val();
        $.ajax({
            url: '@Url.Content("~/Login")',
            dataType: 'json',
            async: false,
            data: {
                userName: userName
            }
        });
    }
}())

在您的登录控制器上操作需要设置cookie

HttpCookie loginCookie = new HttpCookie("loginCookie"); 
Response.Cookies["loginCookie"].Value = userName; 
Response.Cookies.Add(loginCookie);

然后在任何控制器上的Index Action上,您可以检查cookie并进行设置。

HttpCookie cookieUser= Request.Cookies["loginCookie"];
ViewBag.Username = Server.HtmlEncode(cookieUser.Value);