保存表单抛出异常[参数异常" Item with Same Key has been added "]
本文关键字:Key Same has been added with Item 抛出异常 表单 参数 异常 | 更新日期: 2023-09-27 18:03:28
我正在尝试创建一个更新用户信息的表单。我正在使用类型视图的模型类用户:
public class User : MembershipUser, IEntity
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
public virtual Role Role { get; set; }
public virtual string ProfileImage { get; set; }
public virtual string About { get; set; }
}
视图如下所示:
using (Html.BeginForm("SaveUser", "BlogUser", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.ProfileImage)
@Html.HiddenFor(x => x.UserName)
@Html.HiddenFor(x => x.Password)
@Html.HiddenFor(x => x.Role)
<div class="col-sm-10">
<label>Jméno</label>
</div>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Name)
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label>Příjmení</label>
</div>
<div class="col-sm-10">
@Html.TextBoxFor(x => x.Surname, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Surname)
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label>Profilový obrázek</label>
</div>
<div class="col-sm-10">
<input type="file" name="picture" />
@if (!String.IsNullOrEmpty(Model.ProfileImage))
{
<img src="@Url.Content("~/Upload/UserImages/" + Model.ProfileImage)" />
}
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label>Něco o mně</label>
</div>
<div class="col-sm-10">
@Html.TextAreaFor(x => x.About, new { @class = "form-control formatedText", @rows = 20 })
@Html.ValidationMessageFor(x => x.About)
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary btn-lg btn-block">Uložit</button>
</div>
</div>
}
我得到正确的用户,我已经预先填写了正确的详细信息列,但提交后它抛出异常[参数异常"具有相同键的项目已被添加"]。我甚至不会触发SaveUser()控制器方法,所以我至少可以调试它。我不知道问题出在哪里。你能帮我吗?
您可以尝试以下解决方案:已经添加了具有相同键的项
似乎是模型包含相同的属性两次,你可能想要从属性中取出虚拟关键字,如果它是不必要的。
参见:https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx
我明白了。在父类MembershipUser中有一个名为username的属性