4

I have a blogpost edit page where you can either save your edits or upload an image (multiple submits in a single form). When you upload an image, the image link gets appended to a TinyMCE content area.

The fields for the form are in a viewusercontrol(shared with create page). Both the viewpage and usercontrol inherit from BlogPost so the model's being passed directly using <% Html.RenderPartial("Fields", Model); %>

So here's the weird thing; in my controller, when I append the image link to the textarea, nothing happens to the textarea in the view

On my viewpage I have a label for Model.Title and within the usercontrol I have the textbox for editing Model.Title.

If I update the label in the controller - model.Title = "New Title" - the updated model data changes for the label in the viewpage but not the textbox in the usercontrol.

My Controller is like this:

// /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 as to what's causing this and how to fix it? Thanks.

  • I am using 4.0 and MVC 2
zulkamal
  • 578
  • 1
  • 4
  • 17

2 Answers2

3

Turns out that this behaviour is by design and has been answered by Phil Haack here:

Possible bug in ASP.NET MVC with form values being replaced.

There's also a blog post about this here:

ASP.NET MVC’s Html Helpers Render the Wrong Value!

For my scenario (appending an image to tinymce), I think it's safe to clear the ModelState because we're explicitly appending to a textarea and not doing any validation yet.

Community
  • 1
  • 1
zulkamal
  • 578
  • 1
  • 4
  • 17
0

Is there any chance that there is code in the top level view that is changing the value of Model.Title before the RenderPartial is called?

Scrappydog
  • 2,864
  • 1
  • 21
  • 23