1

I'm fairly new to MVC, and I seem to be having a problem returning a view from JS. I'm working on a chatting site, and this code creates a new chatroom with the name you assign it. Here's my JS code:

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

    window.location.href = "/CreateNewChat/CreateConversation?username=" + username + "&convName=" + conversationName;
}

That should call this ActionResult method:

public IActionResult CreateConversation(string username, string convName)
    {

        ChatModel model = new ChatModel(username, convName);
        model.Chatters.Add(username);
        return RedirectToAction("Chat","Chat", model);
    }

Which does, as long as I don't type any conversation name in the text box. If I check the JS code, both username and conversation name values are correctly set up, but the ActionResult method will not be hit if I fill in the conversation name. Here's the Chat method in the ChatController:

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

If I don't put any conversation name in, it will come all the way to this method, but it won't work (as that field is obligatory and I need it to load the view). Any ideas? I've tried using ajax but while it will pass the conversation name along, it will get stuck at the RedirectToAction statement (will never hit the Chat method in the ChatController). I'm really lost here.

1
  • Have you tried passing your model to the Chat action result via TempData instead of as a paramater? Or maybe instead of calling return RedirectToAction("Chat","Chat", model); in CreateConversation just call the Chat action result directly... return Chat(model);. See this post Commented Oct 4, 2019 at 19:04

1 Answer 1

0

Generally, you shouldn't pass objects around like that - at best you should only pass around ViewModels to prevent someone from fudging data and potentially breaking your app.

The way you have it with your redirect will actually push the whole model into the URL parameters before stepping back into code, leaving it wide open for someone to change as they see fit.

Ideally you would post to create a persisted instance then redirect by passing only an identifier to the Chat() method like so:

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 });
    }

ChatController.cs

    public ActionResult Chat(Guid id)
    {
        ChatModel model = yourServiceFactory.GetChatById(id);
        return View(model);
    }

However, if you really want to pass the model onto ActionResult Chat(ChatModel model), then you can execute and return the controller action directly.

    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);
    }

I've created a very simplistic working example of this based on what you've said above, so any validation you may have in your ChatModel constructor is omitted in my example.

For any other errors, I would suggest checking to see if the values you're passing in aren't being detected as "potentially harmful" by the app and being dropped.

Noting that the use of .textContent instead of value would get the full contents between the start and ending tags of a given element, I'm assuming the element is a textarea type not a normal text input. You could be getting extra elements between <textarea>...</textarea>

In my example, my javascript function just takes two local variables declared and set within the function. Without seeing your submit page, I can't really comment on why else setting a value would prevent a call from working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.