テキストボックスがループを介して生成され、デフォルト値が含まれているフォームがあります。しかし、送信時に、更新されたばかりの値ではなく、デフォルト値を取得します。
ここにcshtml
コードがあります
<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>
私がやっている間違いについて何か考えはありますか?ユーザーが以前の値を変更したかどうかを確認できるように、hiddenfor を使用しています。
Edit1: 試してみました
new { Value = Model.Properties[i].Value }
と
new { @Value = Model.Properties[i].Value }
それでもうまくいきませんでした。
Edit2: デフォルトで現在の値が表示されることに気付いたので、textboxfor の新しい部分を削除しました。コントローラの post メソッドで更新された値をまだ取得していません。だから今私は持っている
<div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value)</div>
Edit3:これは物事をより明確にするための画像です
次の画像では、現在の値の最後に 77 を入力してボタンをクリックするだけで、MicrophoneArrayRackModuleId
の値フォーム999888の値を99988877に変更しようとしています。save changes
ボタンをクリックすると、メソッドにsave changes
転送され、メソッドのパラメーターを調べると、図に示されている値が表示されます。99988877だと思っていましたが、999888 と表示されます。HttpPost
module
Edit4: Chrome から開発者ツールを実行する際に、network->httppost method->Headers->formdata を確認したところ、次のように表示されました
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
これは、コントローラーの post メソッドで期待している値ですが、それは起こっていません。
Edit5: Darin が述べたように、名前フィールドをビューに追加したところ、次のようになりました
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>
}
それでもうまくいかない
Edit6: モデル
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; }
}
}
コントローラ
[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" });
}
リポジトリ
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;
}