Strongly typed view with 2 or multiple models
I faced one situation where a single page contains user info as
well as a list of permissions for that user on that page itself, to
do this I think it will be the best practice to strongly type the
page with 2 models at a time. Then when saving we can get 2 values
through models.
Along with this requirement as I have a list of permissions I used
editor template.
GET action for this page:
public
PartialViewResult
UserProfileCreateEdit()
{
UserProfile_ViewModel
upvm = new
UserProfile_ViewModel();
List<Permission_Result>
PermissionList
= new
List<Permission_Result>();
appUser
user = new
appUser();
//get
user info
using
(var
context = new abcEntities())
{
int
userID = Utility.GetInt(Session["UserId"]);
user
= context.appUsers.First(i => i.appUsers_ID == userID);
//set
the existing password- blank
user.appUsers_Password
= "";
//permissions
list by a sp
PermissionList
= context.GetPermissionsByUser(userID).ToList();
}
var
tuple = new
Tuple<List<Permission_Result>,
appUser>(PermissionList,
user);
return
PartialView("UserProfilePartial",
tuple);
}
VIEW for this page, this is partial view in my application
@model
Tuple<List<DAL.Permission_Result>,
DAL.appUser>
@using
(Ajax.BeginForm("UserProfileUpdate",
"DashBoard",
"",
new
AjaxOptions
{
UpdateTargetId
= "ajaxtestP",
OnSuccess
= "Success",
OnFailure
= "Error"
},
new
{ id = "ajaxForm"
}))
{
<div
id="user-profile">
<div
class="modal-header">
<button
type="button"
class="close"
data-dismiss="modal">
×</button>
<h3
id="myModalLabel">
User
Profile</h3>
@Html.HiddenFor(model
=> model.Item2.appUsers_ID)
</div>
<div
>
<div
>
<div>
<label>
User
Name</label>
@Html.TextBoxFor(model
=> model.Item2.appUsers_UserName, new
{ @disabled = "disabled"
})
</div>
<div
>
<label>
Email
id</label>
@Html.TextBoxFor(model
=> model.Item2.miscTelecom.miscTelecom_Email, new
{ @disabled = "disabled"
})
</div>
</div>
<div
>
<div
>
<label>
New
Passowrd</label>
@Html.TextBoxFor(model
=> model.Item2.appUsers_Password, new
{ @type = "password"
})
</div>
<div
class="span6">
<label>
Confirm
Password</label>
<input
name="ConfirmPassword"
type="password"
id="ConfirmPassword"
titlemsg="These
passwords don't match. Try again?"
class="input-block-level"
/>
</div>
</div>
<div
>
<div>
<label>
Role
Name</label>
@Html.TextBoxFor(model
=> model.Item2.admRole.admRole_Name, new
{ @disabled = "disabled"
})
</div>
</div>
<div
>
@Html.EditorFor(Item1
=> Model, "_TemplateAutoAlertList")
</div>
</div>
<div
>
<button
class="btn"
>
Cancel</button>
<button
id="bntsave"
class="btn">
Save</button>
</div>
</div>
}
EditorTemplates for this page
@model
Tuple<List<DAL.Permission_Result>,
DAL.appUser>
<fieldset>
<legend>Permissions</legend>
<table
class="table">
<thead>
<tr
class="table-heading">
<th>
PermissionType
</th>
<th>
PermissionName
</th>
</tr>
</thead>
<tbody>
@for
(int
i = 0; i < Model.Item1.Count; i++)
{
<tr>
<td>
<div
>
@Html.DisplayFor(item
=> Model.Item1[i].PermissionName, new
{ })
@Html.HiddenFor(item
=> Model.Item1[i].PermissionID)
</div>
</td>
<td>
<div
>
@Html.DisplayFor(item
=> Model.Item1[i].PermissionType, new
{ })
</div>
</td>
</tr>
}
</tbody>
</table>
</fieldset>
POST action for this page
[HttpPost]
public
JsonResult
UserProfileUpdate([Bind(Prefix
= "Item1")]
List<Permission_Result>
lstpc, [Bind(Prefix
= "Item2")]
appUser
user)
{
using
(var
context = new
Entities())
{
try
{
-------logic---------
context.SaveChanges();
string
msg = "";
msg
= "Information
Updated";
return
Json(new
{ message = msg, IsSuccess = true
}, JsonRequestBehavior.AllowGet);
}
catch
(Exception
ex)
{
return
Json(new
{ message = ex.InnerException, IsSuccess = false
}, JsonRequestBehavior.AllowGet);
}
}
}