返回视图MVC/Javascript/.NET Core时出现问题

返回视图MVC/Javascript/.NET Core时出现问题,javascript,c#,asp.net,asp.net-mvc,asp.net-mvc-4,Javascript,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我是MVC的新手,似乎在从JS返回视图时遇到了问题。我正在一个聊天网站上工作,这段代码用您指定的名称创建了一个新的聊天室。以下是我的JS代码: function CreateConversation() { var conversationName = document.getElementById("conversationName").value; var username = document.getElementById("usernameLabel").text

我是MVC的新手,似乎在从JS返回视图时遇到了问题。我正在一个聊天网站上工作,这段代码用您指定的名称创建了一个新的聊天室。以下是我的JS代码:

    function CreateConversation() {
    var conversationName = document.getElementById("conversationName").value;
    var username = document.getElementById("usernameLabel").textContent;

    window.location.href = "/CreateNewChat/CreateConversation?username=" + username + "&convName=" + conversationName;
}
应调用此ActionResult方法的:

public IActionResult CreateConversation(string username, string convName)
    {

        ChatModel model = new ChatModel(username, convName);
        model.Chatters.Add(username);
        return RedirectToAction("Chat","Chat", model);
    }
只要我不在文本框中键入任何对话名称,就可以。如果我检查JS代码,username和conversation name值都设置正确,但是如果我填写conversation name,ActionResult方法将不会被命中。 以下是ChatController中的聊天方法:

public IActionResult Chat(ChatModel model)
    {
        return View(model);
    }

如果我没有输入任何对话名称,它会一直用到这个方法,但它不起作用(因为该字段是必需的,我需要它来加载视图)。有什么想法吗?我已经尝试过使用ajax,但是虽然它会传递对话名称,但它会被RedirectToAction语句卡住(永远不会碰到ChatController中的Chat方法)。我在这里真的迷路了。

一般来说,你不应该像那样传递对象-最好只传递ViewModels,以防止有人伪造数据并可能破坏你的应用程序

重定向的方式实际上会将整个模型推到URL参数中,然后再返回到代码中,这样就可以让其他人根据自己的需要进行更改

理想情况下,您可以post创建一个持久化实例,然后通过只将标识符传递给
Chat()
方法重定向,如下所示:

CreateNewChatController.cs

    public ActionResult CreateConversation(string username, string convname)
    {
        Guid chatId = yourServiceFactory.CreateNewChat(username, convname);
        // You passed the service the username anyway so it can do the Chatters.Add() internally

        return RedirectToAction("Chat", "Chat", new { id = chatId });
    }
    public ActionResult Chat(Guid id)
    {
        ChatModel model = yourServiceFactory.GetChatById(id);
        return View(model);
    }
ChatController.cs

    public ActionResult CreateConversation(string username, string convname)
    {
        Guid chatId = yourServiceFactory.CreateNewChat(username, convname);
        // You passed the service the username anyway so it can do the Chatters.Add() internally

        return RedirectToAction("Chat", "Chat", new { id = chatId });
    }
    public ActionResult Chat(Guid id)
    {
        ChatModel model = yourServiceFactory.GetChatById(id);
        return View(model);
    }
但是,如果确实希望将模型传递到
ActionResult Chat(ChatModel model)
,则可以直接执行并返回控制器操作

    public IActionResult CreateConversation(string username, string convname)
    {
        ChatModel model = new ChatModel(username, convname);
        model.Chatters.Add(username);

        ChatController chatController = new ChatController();
        return chatController.Chat(model);
    }
我根据您上面所说的内容创建了一个非常简单的工作示例,因此在我的示例中省略了您的
ChatModel
构造函数中的任何验证

对于任何其他错误,我建议检查您传递的值是否未被应用程序检测为“潜在有害”并被删除

注意,使用
.textContent
而不是value将获得给定元素的开始标记和结束标记之间的完整内容,我假设该元素是textarea类型,而不是正常的文本输入。您可能会在


在我的示例中,我的javascript函数只需要在函数中声明和设置两个局部变量。在没有看到你的提交页面的情况下,我真的无法解释为什么其他设置值会阻止调用工作。

一般来说,你不应该像那样传递对象-最好只传递ViewModels,以防止有人篡改数据并可能破坏你的应用程序

重定向的方式实际上会将整个模型推到URL参数中,然后再返回到代码中,这样就可以让其他人根据自己的需要进行更改

理想情况下,您可以post创建一个持久化实例,然后通过只将标识符传递给
Chat()
方法重定向,如下所示:

CreateNewChatController.cs

    public ActionResult CreateConversation(string username, string convname)
    {
        Guid chatId = yourServiceFactory.CreateNewChat(username, convname);
        // You passed the service the username anyway so it can do the Chatters.Add() internally

        return RedirectToAction("Chat", "Chat", new { id = chatId });
    }
    public ActionResult Chat(Guid id)
    {
        ChatModel model = yourServiceFactory.GetChatById(id);
        return View(model);
    }
ChatController.cs

    public ActionResult CreateConversation(string username, string convname)
    {
        Guid chatId = yourServiceFactory.CreateNewChat(username, convname);
        // You passed the service the username anyway so it can do the Chatters.Add() internally

        return RedirectToAction("Chat", "Chat", new { id = chatId });
    }
    public ActionResult Chat(Guid id)
    {
        ChatModel model = yourServiceFactory.GetChatById(id);
        return View(model);
    }
但是,如果确实希望将模型传递到
ActionResult Chat(ChatModel model)
,则可以直接执行并返回控制器操作

    public IActionResult CreateConversation(string username, string convname)
    {
        ChatModel model = new ChatModel(username, convname);
        model.Chatters.Add(username);

        ChatController chatController = new ChatController();
        return chatController.Chat(model);
    }
我根据您上面所说的内容创建了一个非常简单的工作示例,因此在我的示例中省略了您的
ChatModel
构造函数中的任何验证

对于任何其他错误,我建议检查您传递的值是否未被应用程序检测为“潜在有害”并被删除

注意,使用
.textContent
而不是value将获得给定元素的开始标记和结束标记之间的完整内容,我假设该元素是textarea类型,而不是正常的文本输入。您可能会在


在我的示例中,我的javascript函数只需要在函数中声明和设置两个局部变量。在没有看到提交页面的情况下,我无法解释为什么设置值会阻止调用工作。

您是否尝试过通过TempData而不是作为参数将模型传递给聊天操作结果?或者可以不调用
return RedirectToAction(“Chat”,“Chat”,model)
CreateConversation
中直接调用
Chat
操作结果<代码>返回聊天(型号)。看到这里了吗?您是否尝试过通过TempData而不是作为参数将您的模型传递给聊天操作结果?或者可以不调用
return RedirectToAction(“Chat”,“Chat”,model)
CreateConversation
中直接调用
Chat
操作结果<代码>返回聊天(型号)。看到这个了吗