Hi, when a user logs or registers I retrieve a customProfile model from the database and assign it to the session model binder like so: Is this a good practice?
public ActionResult Register(RegisterModel model, CustomProfile customProfile)
{
//create newCustomProfile code
customProfile = newCustomProfile;
the binder
public class CustomProfileModelBinder : IModelBinder
{
private const string sessionKey = "CustomProfile";
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var CustomProfile = (CustomProfile) controllerContext.HttpContext.Session[sessionKey];
if (CustomProfile == null)
{
CustomProfile = new CustomProfile();
controllerContext.HttpContext.Session[sessionKey] = CustomProfile;
}
return CustomProfile;
}
#endregion
}
the MB stuff makes coding for the web almost as fun as desktop!
funnything is in my register method I get warning that customProfile is not used, even though I just want to assign the customProfile passed into the controler to the newly created one.
I suppose if I'm using the binder for the current logged-in users CustomProfile , I will have to add each change as a seperate param, instead of passing back the new edited CustomProfile as a CustomProfile object?
I guess, I did not understand it, why you need to pass individual value, if you are using a model binder then you can get the values from request object or any other datasource and create the instance of CustomProfile by assigning these values.
I see you are trying to get the CustomProfile value from session and it doesn't exist you are creating a new instance but without any value. You need plug in your datasource to get the value assinged to your custom profile, something like this.
if (CustomProfile == null)
{
CustomProfile = new CustomProfile();
CustomProfile.Property1 = [some values];
CustomProfile.Property2 = [some values2];
....whatever property you want.
controllerContext.HttpContext.Session[sessionKey] = CustomProfile;
}
GorillaMann
Member
117 Points
332 Posts
Model binding practice
Nov 03, 2012 05:24 PM|LINK
Hi, when a user logs or registers I retrieve a customProfile model from the database and assign it to the session model binder like so: Is this a good practice?
public ActionResult Register(RegisterModel model, CustomProfile customProfile)
{
//create newCustomProfile code
customProfile = newCustomProfile;
the binder
public class CustomProfileModelBinder : IModelBinder { private const string sessionKey = "CustomProfile"; #region IModelBinder Members public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var CustomProfile = (CustomProfile) controllerContext.HttpContext.Session[sessionKey]; if (CustomProfile == null) { CustomProfile = new CustomProfile(); controllerContext.HttpContext.Session[sessionKey] = CustomProfile; } return CustomProfile; } #endregion }CPrakash82
All-Star
18720 Points
2899 Posts
Re: Model binding practice
Nov 03, 2012 05:57 PM|LINK
Yes, Model binder is the way to go for this type of operation. The goal is to avoid Session/Request/response in action method.
GorillaMann
Member
117 Points
332 Posts
Re: Model binding practice
Nov 03, 2012 06:23 PM|LINK
thanks for the encouragement!!
the MB stuff makes coding for the web almost as fun as desktop!
funnything is in my register method I get warning that customProfile is not used, even though I just want to assign the customProfile passed into the controler to the newly created one.
customProfile = newCustomProfile;
GorillaMann
Member
117 Points
332 Posts
Re: Model binding practice
Nov 07, 2012 07:20 PM|LINK
I suppose if I'm using the binder for the current logged-in users CustomProfile , I will have to add each change as a seperate param, instead of passing back the new edited CustomProfile as a CustomProfile object?
[HttpPost] public ActionResult Edit(CustomProfile customprofile, string cpField1,string cpField2 ,string cpField3 ) { if (ModelState.IsValid) { //_repository. // _repository.Entry(customprofile).State = EntityState.Modified; // _repository.SaveChanges(); return RedirectToAction("Index"); } return View(customprofile); }CPrakash82
All-Star
18720 Points
2899 Posts
Re: Model binding practice
Nov 08, 2012 01:18 AM|LINK
I guess, I did not understand it, why you need to pass individual value, if you are using a model binder then you can get the values from request object or any other datasource and create the instance of CustomProfile by assigning these values.
GorillaMann
Member
117 Points
332 Posts
Re: Model binding practice
Nov 08, 2012 11:02 AM|LINK
Cool!
any chance you could explain here a bit what I need?
public class CustomProfileModelBinder : IModelBinder { private const string sessionKey = "CustomProfile"; #region IModelBinder Members public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var CustomProfile = (CustomProfile) controllerContext.HttpContext.Session[sessionKey]; if (CustomProfile == null) { CustomProfile = new CustomProfile(); controllerContext.HttpContext.Session[sessionKey] = CustomProfile; } return CustomProfile; } #endregion }CPrakash82
All-Star
18720 Points
2899 Posts
Re: Model binding practice
Nov 08, 2012 12:24 PM|LINK
I see you are trying to get the CustomProfile value from session and it doesn't exist you are creating a new instance but without any value. You need plug in your datasource to get the value assinged to your custom profile, something like this.
if (CustomProfile == null) { CustomProfile = new CustomProfile(); CustomProfile.Property1 = [some values]; CustomProfile.Property2 = [some values2]; ....whatever property you want. controllerContext.HttpContext.Session[sessionKey] = CustomProfile; }GorillaMann
Member
117 Points
332 Posts
Re: Model binding practice
Nov 10, 2012 11:15 AM|LINK
The thing is that I only want the CustomProfile new'ed up like this when they arn't logged in.
when they log in I assign the custom profile using the entity framework
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl, CustomProfile customProfile)
{
if (ModelState.IsValid)
{
if (authProvider.Authenticate(model.UserName, model.Password, model.RememberMe))
{
//point to the correct customProfile of the db
customProfile = customProfileProvider.Find(model.UserName);
return RedirectToAction("Index", "Home");
GorillaMann
Member
117 Points
332 Posts
Re: Model binding practice
Nov 10, 2012 11:20 AM|LINK
I dont want to assign these each time in the model binder, only when edited action is called
[HttpPost] public ActionResult Edit(CustomProfile customprofile, string cpField1,string cpField2 ,string cpField3 ) {CPrakash82
All-Star
18720 Points
2899 Posts
Re: Model binding practice
Nov 10, 2012 12:21 PM|LINK
You can do something like this at action level -
public ActionResult Edit([ModelBinder(typeof(CustomProfileModelBinder))] CustomProfile customprofile,...