bombai
bombai

Reputation: 967

no parameterless constructor defined for this object MVC3

i have searched and tried about my problem and i cant find the solution, i want to know if somebody can help me to fix my code..

I have a view to edit the users..

 @model MembershipUser
 @{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
 }

@using (Html.BeginForm()) {

@Html.ValidationSummary(true)
<fieldset>  
    <div class="editor-label">
      Username:
    </div>
    <div class="editor-field">
    @Html.DisplayFor(model => model.UserName)
    </div>

    <div class="editor-label">
       Email:
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Email)
    </div>

 <div class="editor-label">
       Password:
    </div>
    <div class="editor-field">
   @Html.DisplayFor(model => model.Password) 
    </div>
          <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

And then in the controller a action to can get the user info..

    [Authorize(Roles = "Admins")]
    public ActionResult Edit(string username)
    {
        var usu = Membership.GetUser(username);
        return View(usu);
    }

And another to can update the user..

    [Authorize(Roles = "Admins")]
    [HttpPost]
    public ActionResult Edit(MembershipUser umtoupdate)
    {

MembershipUserCollection iduser = Membership.GetAllUsers();            

        if(umtoupdate.Password != umtoupdate.ConfirmPassword)
        {
                alert = "Password dont match!";
                return View("Users", "Admin");
        }

        else {

            foreach (MembershipUser um in iduser) {

                if (um.UserName == umtoupdate.UserName) {

                    Membership.DeleteUser(um.UserName, true);

                    // Attempt to register the new updated user
                    MembershipCreateStatus createStatus;
                    Membership.CreateUser(umtoupdate.UserName,
                    umtoupdate.Password,
                    umtoupdate.Email,
                    umtoupdate.PasswordQuestion,
                    umtoupdate.AnswerQuestion, true, null, out createStatus);

                }
            }
        }

               return View("Details", umtoupdate.UserName);

     }

And in this class, i have defined one constructor that creates a list to do another things(i put because it tell me something about a parameterless constructor)..

    public AdminController()
    {

    foreach (MembershipUser member in iduser) {


            UserEntity entity2 = new UserEntity();
            entity2.Username = member.UserName;
            entity2.Roles = "Users";
            entity2.Email = member.Email;
            for (int i = 1; i < iduser.Count; i++)
            {
                entity2.ID_client = i;
            }
            _entities.Add(entity2);

       }

And then, i go to edit a user, i can go to the first view (where can edit the info) but when i try to save the updated info and go to the second view to update the user, it tell me

"[MissingMethodException no parameterless constructor defined for this object."

Can someone tell me how can i fix it? If i change the Edit(string) and delete the string parameter, but in this case I cant get the info user, and if i add another view without parameter, it dont work for ambiguity..

Thanks.


Ok thanks i didnt know that it havent a public constructor! But if i do another inherit class from MembershipUser (ex, MyUser as you said, with a public constructor) and i do

public ActionResult Edit(MyUser umtoupdate) {...}

When i try to get the user info tell me that im using a MembershipUser dictionary instead of MyUser dictionary because with the another view

[Authorize(Roles = "Admins")]
public ActionResult Edit(string username)
{
    var usu = Membership.GetUser(username);
    return View(usu);
}

Use the membeship class and return a membership user and i cant use in this case MyUser.GetUser because is a method from Membership!

What can i do to fix it?

Thanks a lot.

Upvotes: 2

Views: 6320

Answers (3)

Mark
Mark

Reputation: 6081

I've done this by setting the value within my model objects constructor

Model Example:

public class ViewModel
{
    public MembershipUser User { get; set; }

    public ViewModel()
    {
        User = Membership.GetUser();
    }
}

Example POST action:

[HttpPost]
public ActionResult Account(ViewModel model)
{
    Membership.UpdateUser(model.User);
    return View(model);
}

Upvotes: 0

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

MembershipUser doesn't have a default public no-parameter constructor, so you'll have to wrap the object in another class to make it accessible to your view as a model.

public class MyUserModel
{
    public MembershipUser User { get; set; }
}

In your controller:

MyUserModel model = new MyUserModel { User = Membership.GetUser() };

Upvotes: 2

Michal B.
Michal B.

Reputation: 5719

ModelBinder tries to bind your form to MembershipUser. Unfortunately MembershipUser does not have a public default constructor. This is a requirement if you want to use the ModelBinder...

I would suggest creating some other class (e.g. MyUser) and use this class as your ViewModel.

Upvotes: 6

Related Questions