简体   繁体   English

我可以使用ASP.NET MVC4作为ActionResult发送消息而不是视图吗

[英]Can I send a message instead of a view back as an ActionResult with ASP.NET MVC4

I have the following action in ASP.NET MVC4 我在ASP.NET MVC4中有以下操作

    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                // ?? Need some code here 
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

I have the following code that calls this: 我有以下代码调用此代码:

$('#article').on('submit', '#loginForm, #registerForm', function (e) {
     e.preventDefault();
     var $form = $(this);
     var href= $form.attr('data-href');
     $form.validate();
     if (!$form.valid()) {
         return false;
     }
     var data = $form.serializeArray();
     $.ajax(
     {
         data: data,
         type: 'POST',
         url: href
     })
         .done(submitDone)
         .fail(submitFail);
     function submitDone(content) {
         $('#article').html(content)
     }
     function submitFail() {
         alert("Failed");
     }
     return false;
 });

If the registration works I would like to force the whole web page to refresh. 如果注册有效,我想强制刷新整个网页。 Is there a way that I can send back a message from the actionmethod to the javascript to tell it that the registration works and the javascript should refresh the whole web page? 有没有一种方法可以将动作方法中的消息发送回javascript以告知注册成功,并且javascript应该刷新整个网页?

I did try return RedirectToLocal("/"); 我确实尝试过返回RedirectToLocal(“ /”); but this definitely does not work. 但这绝对行不通。 What this does is to return a new page and then have it populated in the #article DIV. 这是要返回一个新页面,然后将其填充在#article DIV中。

There is nothing that will automatically refresh the browser from the server. 没有什么可以自动从服务器刷新浏览器。

To refresh the browser from the server you'll need to send something from the server to the client indicating that you want to refresh the page. 要从服务器刷新浏览器,您需要将一些内容从服务器发送到客户端,以表明您要刷新页面。 You'll need to write the javascript to look for the indication to refresh the browser. 您需要编写JavaScript来查找刷新浏览器的指示。

Client Code 客户代码

function submitDone(content) {

 var json = $.parseJson(content);

  if(json.isSuccess) {

   //Do something here   

  }

     $('#article').html(json.content)

 }

Server code 服务器代码

public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                // ?? Need some code here 
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

    // If we got this far, something failed, redisplay form
    return Json(new {isSuccess = true, content = model});
}

I am unsure of what you are trying to accomplish by refreshing the page, if it's to clear out the form fields. 我不确定您要通过刷新页面来完成什么(如果要清除表单字段)。 The same could be achieved by using JavaScript. 通过使用JavaScript可以实现相同的目的。 By using javascript instead of a page refresh you won't lose page state, such as error messages. 通过使用javascript而不是页面刷新,您不会丢失页面状态,例如错误消息。

well i can think of a quick javascript trick to refresh a page on success like this 好吧,我可以想到一个快速的JavaScript技巧来像这样成功刷新页面

function submitDone(content) {
     window.location.reload();
 }

this will reload the page on the success. 成功后将重新加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM