Guilherme Longo
Guilherme Longo

Reputation: 2308

Building model in Controller

I have a link that opens a dialog to user to add it´s profile.

I want the data to be there on hidden fields

The controller calls this view but I am not sure how to populate my model in controller and then use the model in this view to set the values properties.

this is my controller and as you can see I am using ViewBag and I don´t want that.

public ActionResult EditUser()
    {
        try 
        {
            MembershipUser membershipUser = Membership.GetUser(Request.QueryString["username"]);
            ViewBag.user = membershipUser;

            return PartialView();

        }
        catch (Exception e)
        {   
            return Content("Error: " + e);
        }
    }

Could you please help me understand how to build my model and use it in my view? Thanks a lot...

Upvotes: 1

Views: 96

Answers (3)

Amit Sharma
Amit Sharma

Reputation: 111

You have to add a model name UserModel and define user object there

 public class UserModel
        {   
            public User UserInfo { get; set; }

        }

Now add @model UI.Web.App.Models.UserModel top of the Partialview in EditUser.cshtml

after that change your controller code as well-

public ActionResult EditUser()
    {
        try 
        {
            MembershipUser membershipUser = Membership.GetUser(Request.QueryString["username"]);
            UserModel usermodel=new UserModel();
            usermodel.UserInfo = membershipUser;
            return PartialView(usermodel);

        }
        catch (Exception e)
        {   
            return Content("Error: " + e);
        }
    }

It will remove view bag from controller. hoping this will help you, if there is any problem please let me know.

Upvotes: 0

HTX9
HTX9

Reputation: 1717

You can simply pass theMembershipUser to the view and use that as the model. Your controller action method will retrieve the user and return it to the view:

MembershipUser membershipUser = Membership.GetUser(Request.QueryString["username"]);
return PartialView("PartialViewName", membershipUser); // Specify the partial view name and pass the membership user as a model

Then in your view add the @model directive and access the properties of MembershipUser:

@model MembershipUser

// If you want to use a hidden field for a property
@Html.HiddenFor(m => m.SomeProperty)

// Uses the default editor template for the data type to allow user to edit the property
@Html.EditorFor(m => m.SomeProperty)

 // If you just want to show the property
@Model.SomeProperty

Upvotes: 1

Owen
Owen

Reputation: 501

All you have to do is pass the model you've created into the call to the View/Partial View. So in your instance rather than calling:

ViewBag.user = membershipUser

you should use:

return PartialView(membershipUser);

You just need to make sure that in your view you have a @model declaration at the top. It'll be something like:

@model MembershipUser;
<p>@Model.SomeField</p>

Upvotes: 4

Related Questions