I have a form, whose textboxes are generated via loop and contains its default value. But upon submission, I get its default value and not the value that was just updated.
Here is the cshtml
code
<fieldset>
<legend>Module <small>Edit</small></legend>
@using (Html.BeginForm("Edit", "Module"))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m=>m.Id)
for(var i = 0; i < Model.Properties.Count(); i++)
{
@Html.HiddenFor(model=>Model.HiddenProperties[i].Value)
<label class="label">@Model.Properties[i].Name</label>
<div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value, new { @value = Model.Properties[i].Value })</div>
}
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
@Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
</div>
}
</fieldset>
Any idea on what mistake I'm doing? I'm using hiddenfor so that I can check if user has changed the previous value.
Edit1: I tried
new { Value = Model.Properties[i].Value }
and
new { @Value = Model.Properties[i].Value }
Still didn't work.
Edit2: just realised it shows current value by default, so i removed the new part in textboxfor. Still don't get the updated value in post method of the controller. So now i have
<div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value)</div>
Edit3: here is the image to make things more clear
now in following image I try to change the value of MicrophoneArrayRackModuleId
's value form 999888 to 99988877 just by typing 77 at the end of current value and i click save changes
button
after clicking save changes
button i get forwarded to HttpPost
method, and upon inspecting the module
parameter in the method I see the value that is shown in picture. I was expecting it to be 99988877 but it shows 999888
Edit4: On running developers tool from chrome, I checked network->httppost method->Headers->formdata and this is what it shows
Id:50
HiddenProperties[0].Value:999888
Properties[0].Value:99988877
HiddenProperties[1].Value:000-00000-000
Properties[1].Value:000-00000-000
HiddenProperties[2].Value:1a2b3c
Properties[2].Value:1a2b3c
which is the value i'm expecting in post method of controller but thats not happening.
Edit5: as mentioned by Darin I added name field in view and it now looks like
for(var i = 0; i < Model.Properties.Count(); i++)
{
@Html.HiddenFor(model=>model.HiddenProperties[i].Name)
@Html.HiddenFor(model=>model.HiddenProperties[i].Value)
<label class="label">@Model.Properties[i].Name</label>
<div class="input-block-level">@Html.TextBoxFor(model => model.Properties[i].Value)</div>
}
still it doesn't work
Edit6: Model
public class PropertyViewModel
{
public string Name { get; set; }
public string Value { get; set; }
}
public class EditModule
{
public long Id { get; set; }
public List<PropertyViewModel> Properties { get; set; }
public List<PropertyViewModel> HiddenProperties
{
get { return Properties; }
set { Properties = value; }
}
}
Controller
[HttpGet]
public ActionResult Edit(long id)
{
var module = _repository.GetModuleProperties(id);
return View(module);
}
[HttpPost]
public ActionResult Edit(EditModule module)
{
if (ModelState.IsValid)
{
_repository.SaveModuleEdits(module);
Information("Module was successfully edited!");
return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
}
Error("Edit was unsuccessful, if the problem persists please contact Merijn!");
return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });
}
Repository
public EditModule GetModuleProperties(long id)
{
var properties = new EditModule
{
Id = id,
Properties =_dbSis.Modules.Where(t => t.Id == id)
.SelectMany(m => m.PropertyConfiguration.PropertyInstances.Select(i => new PropertyViewModel
{
Name = i.Property.Name,
Value = i.Value
})).ToList()
};
return properties;
}
You never sent the Name property, that's why it is always NULL. Make sure you include it as a hidden field:
Also do not capture the
Model
in a closure. Use themodel
variable instead when writing your expressions:Also you absolutely don't need to be setting the
Value
attribute, that is what the Html helper is for:Write:
in place of:
just leave it like this: