Returning a ViewModel in MVC

Some time it happes when we want to display certain Information in form according to some ID.
This form may contain some Dropdown fields some textboxes or may be other web controls, these controls should display
proper information according to to that ID.
So Instead of writing this code on the Controller class (making controller massy 😦 ) or a smart way could be
creating a ViewModelClass and retutn it.

What a ViewModelClass contains is all the methods and functions which are required to fill all controls according to ID.

Suppose I have a view contains different Dropdowns and textboxes.
I will create a ViewModelClass for this as below :

 public class AddEditScheduleViewModel
    {
        #region Public Properties
        public SelectList ddlClient { get;  set; }
        public SelectList ddlStaff { get;  set; }
        public string txtScheduleDate { get;  set; }
        #endregion 

        #region Contructor
        public AddEditScheduleViewModel()
        {
            ddlClient = new SelectList(ClientsDAL.GetClientList(SessionHandler.LoggedinUserInfo.AgencyID, "02"), "clientID", "entDisplayName");
            ddlStaff = new SelectList(StaffDAL.GetStaffList(SessionHandler.LoggedinUserInfo.AgencyID), "staffID", "staffName");
            txtScheduleDate = DateTime.Now.ToShortDateString();
        }
	#endregion 
     }

Here we have made one Constructor “AddEditScheduleViewModel()” where we have written code to get data for dropdown.

<%=Html.DropDownList("ddlClient",(SelectList)ViewData["ddlClient"], "", new { onChange="return onClientChange(this)" , @Style = "width:65%" })%>
<%=Html.DropDownList("ddlStaff",(SelectList)ViewData["ddlStaff"], "", new { onChange="return onStaffChange(this)", @Style = "width:65%" })%>

This ViewModel may also contains some method Overloads like below :

public AddEditScheduleViewModel(string ScheduleID)
{
    getSchedulDataByID(ScheduleID, "none");
}
public AddEditScheduleViewModel(string ScheduleID,string mode)
{
    getSchedulDataByID(ScheduleID,mode);
}

Now how to use this ViewModelClass and how to call these Overload methods :

public ActionResult AddEditSchedule(string Submit) { 
switch (Submit)
    {
	case "Save":
		AddSchedule(ScheduleID); //Add Edit Schedule Event
		return View(new AddEditScheduleViewModel());
	case "Delete":
		DeleteSchedule(ScheduleID);
		return View(new AddEditScheduleViewModel());
	case "Show Conflict":
		return View(new AddEditScheduleViewModel(Request.Form["hdnConflictSchedID"].ToString()));
	default:
	    //your custom code will go here
    }
}
Advertisements

2 responses to “Returning a ViewModel in MVC

  1. Paul October 26, 2013 at 7:48 am

    Howdy, I do believe your blog might be having internet browser compatibility problems.
    Whenever I take a look at your blog in Safari, it looks
    fine however, when opening in I.E., it has some
    overlapping issues. I simply wanted to give you a quick heads up!
    Other than that, fantastic site!

    • Abhishek October 30, 2013 at 5:48 am

      Hay thanks Paul,
      and thanks for observing and letting me know this behavior. May I know which version of IE browser you are using. I am using IE8 and works fine.
      Thank you once again..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: