Updating viewmodel data in the controller does not reflect input fields

I have a blog editing page in which you can save your changes or upload an image (several posts in one form). When you upload an image, an image link is added to the TinyMCE content area.

The fields for the form are in the viewusercontrol (together with the created page). Both the viewpage and usercontrol inherit from BlogPost, so the model is passed directly using<% Html.RenderPartial("Fields", Model); %>

So, here is a strange thing; in my controller, when I add a link to an image in a text box, nothing happens with the text box in the view

On my browse page, I have a label for Model.Title, and inside usercontrol I have a text box for editing Model.Title.

If I update the label in the controller - model.Title = "New Title"- the updated model data changes for the label on the browse page, but not in the text field in usercontrol.

My controller is as follows:

// /edit/{id}
public ViewResult Edit(int id, BlogPost model, string submit)
    {
        if (ModelState.IsValid)
        {
            switch (submit)
            {
                case "Upload":
                    var files = UploadFiles(Request.Files); // uploading works

                    model.Content += files[0].Link; // model is updated but not cascaded at runtime
                    model.Title = "Test"; // Force a title change to reproduce the issue
                    return View(model);

                default:
                    repository.Update(model);
                    break;
            }
        }

        return View(model);
    }

Any ideas on what causes this and how to fix it? Thank.

  • I am using 4.0 and MVC 2
+3
source share
2 answers

It turns out that this is a design behavior, and Phil Haack answered this question:

Possible error in ASP.NET MVC with replacing form values.

There is also a blog post about it here:

ASP.NET MVCs Helpers Html Render the Wrong Value!

( tinymce) ModelState, .

+2

, , Model.Title RenderPartial?

0

Source: https://habr.com/ru/post/1761552/


All Articles