1

I only need to update one field in the database, the name from a form. I use hidden fields to update/keep the value of the rest. This i my view

@using (Ajax.BeginForm("ChangeHeaderName", "Rooms",
                            new AjaxOptions
                            {
                                InsertionMode = InsertionMode.Replace,
                                HttpMethod = "Post",
                                OnSuccess = "OnSuccess",
                                OnFailure = "OnFailure"
                            }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.RoomId)
    @Html.HiddenFor(model => model.RoomLink)
    @Html.HiddenFor(model => model.Alias)

    <div class="room-header-container">
        <h2>@Html.EditorFor(m => m.Name, new { htmlAttributes = new { @id = "room-header", @class = "edit-header" } })</h2>
    </div>
}

Feels stupid to use @Html.HiddenFor for every field I don't want to update.

This is my controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ChangeHeaderName(Room room)
{
    if (ModelState.IsValid)
    {
        db.Entry(room).State = EntityState.Modified;
        db.SaveChanges();

        return PartialView("_UpdateHeader", room);
    }
    return View(room);
}

Is there a better solution without hidden fileds?

Edit/Update

I have this in my model

public int RoomId { get; set; }
public string Name { get; set; }
public Guid RoomLink { get; set; }
public bool UseAlias { get; set; }
public string Alias { get; set; }
public DateTime Created { get; set; }

And I want to display all of this to the user, but the user should only be able to edit Name

If I create a ViewModel with only Name

public string Name { get; set; }

Can't I display the rest. I want to display all but only update Name

Anders
  • 375
  • 2
  • 5
  • 18
  • Absolutely. As **always**, use a view model with only the properties you need, and in the POST method, get the data model, map the view model properties to it and save the data model. –  Feb 23 '16 at 10:24
  • @StephenMuecke Ok, but I need to display more fields from the database but the user only has the rights to update one of them (Name). I can't display data from other fields if I use ViewModel (with only name). I have updated my question. – Anders Feb 23 '16 at 14:57

2 Answers2

1

Found solution here: https://stackoverflow.com/a/23380525/5369591

if (ModelState.IsValid)
{
    db.Registrations.Attach(registration); // attach in the Unchanged state
    db.Entry(registration).Property(r => r.Date).IsModified = true;
    // Date field is set to Modified (entity is now Modified as well)
    db.SaveChanges();
    return RedirectToAction("Index");
}
Community
  • 1
  • 1
Anders
  • 375
  • 2
  • 5
  • 18
0

Yes, you can put disabled attribute for all field which you want to Ignore.

With Jquery you can do this way

$(document).ready(function(){

$('input[type="hidden"]').attr("disabled","disabled");

})
Shobhit Walia
  • 496
  • 4
  • 19
  • And that would mean that the values would not be submitted and when saving, `null` or default values would be saved for all those properties, completely screwing up the application! (read the question again and understand what OP is doing) –  Feb 23 '16 at 11:17