同一页面ASP.net MVC中的多个表单

时间:2012-04-03 16:55:02

标签: asp.net-mvc

我正在开发我的第一个ASP.net MVC项目,并且在同一页面中使用多个表单时出现了一些问题。 首先我创建了2个部分类:   (*注册将允许用户注册,    *登录,允许用户登录。)

然后我使用HTML.render将它们集成到我的“Logpage”中。 所以我要使用2个不同的动作。像这样:

        [HttpPost]
    public ActionResult Login(LogModel.Login Model)
    {
        if (ModelState.IsValid)
        {

            if (LogModel.Login.Verifuser(Model.IDUser, Model.Password))
            {
                FormsAuthentication.SetAuthCookie(Model.IDUser, false);
                if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
                {
                    return View("Admin/Index");
                }
                else
                {
                    return View("Agence/Index");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalide username or Password");
                return View(Model);
            }
        }
        return View(Model);
    }

在错误情况下我重定向到新页面的问题(白页包含验证摘要)。所以我想知道如何在我的默认页面Logpage中显示此错误消息。

3 个答案:

答案 0 :(得分:26)

您可以使用三个操作和一个复杂的模型来解决此问题。

 public class LoginOrRegisterViewModel
 {
      public Models.RegisterViewModel Register { get; set; }
      public Models.LoginViewModel Login { get; set; }
 }


 [HttpGet]
 public ActionResult Login()
 {
      return View("Login", new LoginOrRegisterViewModel());
 }

 [HttpPost]
 public ActionResult Register(Models.LoginViewModel model)
 {
      if(!ModelState.IsValid)
           return View("Login", new LoginOrRegisterViewModel(){ Register = model });
      else
      {
           //TODO: Validate the user
           //TODO: Write a FormsAuth ticket
           //TODO: Redirect to somewhere
     }
 }

 [HttpPost]
 public ActionResult Login(Models.RegistrationViewModel model)
 {
      if(!ModelState.IsValid)
           return View("Login", new LoginOrRegisterViewModel(){ Login = model});
      else
      {
           //TODO: CRUD for registering user
           //TODO: Write forms auth ticket
           //TODO: Redirect
     }
 }

在您的代码中,请确保设置表单的操作:

 @model Models.LoginOrRegisterViewModel

 @using(Html.BeginForm("Login", "Controller", FormMethod.Post, new { id = "loginForm"}))
 {
       @Html.EditorFor(m => Model.Login)
 }

 @using(Html.BeginForm("Register", "Controller", FormMethod.Post, new { id = "registerForm"}))
 {
       @Html.EditorFor(m => Model.Register)
 }

答案 1 :(得分:0)

看起来你还需要在!Verifyuser分支中调用IsAdmin,让它返回正确的视图:

        if (ModelState.IsValid)
        {

            if (LogModel.Login.Verifuser(Model.IDUser, Model.Password))
            {
                FormsAuthentication.SetAuthCookie(Model.IDUser, false);
                if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
                {
                    return View("Admin/Index");
                }
                else
                {
                    return View("Agence/Index");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalide username or Password");

                if (LogModel.Login.IsAdmin(Model.IDUser, Model.Password))
                    return View("Admin/Index", Model);
                else
                    return View("Agence/Index", Model);

            }
        }

目前,当您遇到ModelState错误时,您正在调用return View(Model);,该错误会返回操作的默认视图,称为“登录”。

答案 2 :(得分:0)

如果您希望多个表单同时发布到控制器操作,请使用客户端 JS 创建一个 JSON 对象,该对象为每个表单提供一个单独的属性,并为每个属性指定一个驼峰式名称,该名称对应于您的相应 ProperCase 参数控制器动作。然后,将该 JSON 对象的序列化发送到您的控制器:

let form1 = $('#my-form1-id');
let form2 = $('#my-form2-id');
let url = 'myArea/myController/myAction'
let viewData = {
    controllerParameter1: form1,
    controllerParameter2: form2
};

$.ajax({

    url: url,
    type: 'POST',
    data: JSON.stringify(viewData),
    dataType: "json",
    beforeSend: function () {
        $('#my-submit-button-id').html('<i class="fa fa-2x fa-cog fa-spin"></i>');
        $('#my-submit-button-id').attr("disabled", "disabled");
    },
    success: function (res) {
        alert("Submitted!");
    },
    error: function (status, err) {
        alert("Failed!");
    },
    complete: function (status, err) {

        $('#my-submit-button-id').html('Submit My 2 Forms');
    }
});

这应该适用于带有类似于以下签名的控制器操作:

[HttpPost]
public virtual async Task<ActionResult> MyAction(ViewModel1 controllerParameter1, ViewModel2 controllerParameter2)
{
    // do amazing things
}
相关问题