Tag Archives: view

ASP.NET MVC with models inheritance and views


Recently I bumped into a intersing question on stackoverflow.com. The problem was that user had a view but wanted to have some input elements based on variable model. I mean if model passed to view is a, render a Email text input and if it is b then render Name text input. But in mvc you can pass only one model to view. So I suggested have a base model and base view and by checking the type of model in view load the related view using partial as below:

 public ActionResult Base()
            return View(new DerviedOne());

 public class BaseModel
        public int Id { get; set; }

    public class DerviedOne : BaseModel
        public string Email { get; set; }

    public class DerviedTwo : BaseModel
        public string Name { get; set; }
 @using Models
@model Models.BaseModel

    ViewBag.Title = "Base";
    Layout = "~/Views/Shared/_Layout.cshtml";


@using (Html.BeginForm()) 

<div class="form-horizontal">


        @Html.ValidationSummary(true, "", new {@class = "text-danger"})

<div class="form-group">

<div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default"/>



    if(Model is DerviedOne)
         Html.RenderPartial("DerviedOneView", Model as DerviedOne);

    if (Model is DerviedTwo)
        Html.RenderPartial("DerviedTwoView", Model as DerviedTwo);

Restricting views based on security roles even for quick search view


In a company I currently work (I told you before), we have lots of staff and teams that each staff belongs to one of them. To apply security rules to CRM system, we have already some tools, like “Security Role” and “Field Level Security”.

You can define which entities a team or user can access an how far. I mean you can restrict a user to see only his/her activities or hide some entities for him/her.

But there are some situations that you want to show only specific view of an entity to a team. Say you want to only names and owner of “Account” entity to team “X”.

When a user opens “Accounts”, bunch of views are there to filter result for him/her. But we want user to have only one view of “Account” and all other views being hidden from her/him.

I googled and found this useful solution. You can easy filter views based on security rules. You can even say which view is the default one, when a user/team wants to see records of some entity.

Using the solution, I created a system view, assigned to team “X” as a default view for “Accouint”. Every thing works fine, team members only see name and owner in their views, but when they use quick search box, view changes and all other fields become visible for team members.

That is where plug-in comes to the play. By creating a plugin on pre-operation of “RetriveMultiple” message of account entity you can restrict the fields returned to the user requested the view.

Hope it helps.