我有一个模型,你可以在这里看到:
public partial class AssistanceJuror
{
public int Id { get; set; }
public System.DateTime Date { get; set; }
public int UserId { get; set; }
public int AssistanceId { get; set; }
public virtual User User { get; set; }
public virtual Assistance Assistance { get; set; }
}
我的控制器中有一个操作可以返回edit
视图。
@model DomainClass.AssistanceJuror
@{
ViewBag.Title = "View1";
Layout = "~/Views/Shared/_LayoutIdeaOtherPage.cshtml";
}
<h2>View1</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AssistanceJuror</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserId)
@Html.ValidationMessageFor(model => model.UserId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssistanceId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssistanceId)
@Html.ValidationMessageFor(model => model.AssistanceId)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我的编辑操作如下:
public ActionResult EditExpert(int id)
{
ViewBag.listAssistance = new SelectList(objAssistanceRepository.ReturnAssitanceWithoutExpert(), "Id",
"AssistanceName");
AssistanceJuror t = new AssistanceJuror();
if (objAssistanceJurorRepository.FindBy(i => i.UserId == id).Count() == 1)
{
t = objAssistanceJurorRepository.FindBy(i => i.UserId == id).First();
}
return View(t);
}
我的帖子
[HttpPost]
[Authorize(Roles = "Admin,Expert")]
public ActionResult EditExpert(AssistanceJuror assistanceJuror)
{
}
让我在edit
操作中解释我的问题我使用AssistanceJuror
将id=5
返回到我的视图,但正如您可以看到我在AssistanceJuror.id
中保留@Html.HiddenFor(model => model.Id)
},但点击提交按钮后,视图返回的ID将更改为4
。
最好的问候
我改变了这个:
@Html.HiddenFor(model => model.Id)
到此:
<div class="editor-label">
@Html.LabelFor(model => model.Id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
</div>
文本框中显示的值为4
,表示用户ID。
执行edit get maction后,我的html代码输入值如下:
<form action="/Assistance/EditExpert/4" method="post"> <fieldset>
<legend>AssistanceJuror</legend>
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="4" />
<div class="editor-label">
<label for="UserId">UserId</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field UserId must be a number." data-val-required="The UserId field is required." id="UserId" name="UserId" type="number" value="4" />
<span class="field-validation-valid" data-valmsg-for="UserId" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Date">Date</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="Date" name="Date" type="datetime" value="8/28/2014 12:38:01 PM" />
<span class="field-validation-valid" data-valmsg-for="Date" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="AssistanceId">AssistanceId</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field AssistanceId must be a number." data-val-required="The AssistanceId field is required." id="AssistanceId" name="AssistanceId" type="number" value="4" />
<span class="field-validation-valid" data-valmsg-for="AssistanceId" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
</form>
答案 0 :(得分:0)
我终于找到了问题。问题是参数的名称:
在get方法中,我可以看到一个id:
public ActionResult EditExpert(int id)
{
ViewBag.listAssistance = new SelectList(objAssistanceRepository.ReturnAssitanceWithoutExpert(), "Id",
"AssistanceName");
AssistanceJuror t = new AssistanceJuror();
if (objAssistanceJurorRepository.FindBy(i => i.UserId == id).Count() == 1)
{
t = objAssistanceJurorRepository.FindBy(i => i.UserId == id).First();
}
return View(t);
}
所以当id传递给这个动作时,mvc会尝试将这些值与你的模型匹配,这就是mvc试图把id放到模型id的问题,但实际上我得到的这个值是用户id之后将参数名称更改为userid已解决的问题:
public ActionResult EditExpert(int userid)
{
ViewBag.listAssistance = new SelectList(objAssistanceRepository.ReturnAssitanceWithoutExpert(), "Id",
"AssistanceName");
AssistanceJuror t = new AssistanceJuror();
if (objAssistanceJurorRepository.FindBy(i => i.UserId == userid).Count() == 1)
{
t = objAssistanceJurorRepository.FindBy(i => i.UserId == userid).First();
}
return View(t);
}