1

I am trying to create a form for updating user info. I am using typed view for model class User:

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; }

    }

The view looks like this:

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>
}

I am getting right user, i have prefilled columns with right details, but after submit it throws exception [Argument Exception “Item with Same Key has already been added”]. I wont even trigger the SaveUser() controller method, so I could atleast debug it. I cant figure out where the problem is. Can you help me?

Pogasta
  • 117
  • 1
  • 14

2 Answers2

0

You could try the following solution: An item with the same key has already been added

It seems like it is the model contains a same property twice, you may want to take out the virtual keyword from properties if it is not necessary.

See also: https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

Community
  • 1
  • 1
Zay Lau
  • 1,856
  • 1
  • 10
  • 17
  • You may be somewhat confusing `virtual` and `new`, the latter being mentioned in the linked SO answer - using virtual and overriding instead of `new` is in fact suggested by that answer as unlike `new` it *prevents* double properties. Also, it's not stated explicitly, but given the tags the `virtual` is probably there for best functionality with NHibernate. – Oskar Berggren Aug 22 '16 at 08:55
0

I figured it out. I had property called username in parental class MembershipUser.

Pogasta
  • 117
  • 1
  • 14