Model in Layout breaks other pages

前端 未结 1 373
不思量自难忘°
不思量自难忘° 2021-01-28 16:36

I have a design flaw based on my lack of MVC4 experience.

The issue is that I have a model in my Layout...

@model BasicFinanceUI.Models.LoginModel


        
相关标签:
1条回答
  • 2021-01-28 17:07

    First build the view like you have it but instead of using helpers just build the html fields. Make sure you put an id or a class on the fields that we can use as a selector

    <input type="text" class="txtUserName" /> etc
    

    then make sure you have jquery referenced on the page and put this on the bottom of your screen

    <script type="text/javascript">
        $(document).ready(function(){
            $('.btnSubmit').on('click', function(){
                $.ajax({
                    url: "@(Url.Action("Action", "Controller")",
                    type: "POST",
                    contentType: "application/json",
                    data: { UserName: $('.txtUserName').val(), Password: $('.txtPassword').val() }
                    cache: false,
                    async: true,
                    success: function (result) {
                        alert('Login Successful!');
                        window.location = "@Url.Action("Index", "Controller")";
                    }
                });
            });
        });
    </script>
    

    then on your controller you need to have a method set up to receive the ajax call

    [HttpPost]
    public ActionResult Login(string UserName, string Password){
        //Check the user name and password against the database
        //from here http://stackoverflow.com/questions/10608198/asp-net-mvc3-returning-success-jsonresult
        var result=new { Success="True", Message="Success"};
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    
    0 讨论(0)
提交回复
热议问题