niknaz
niknaz

Reputation: 47

how can pass a model to some partial view

i have a page that contains 3 partial views.

    ...
    .
    .
      @Html.Action("one" , "Home")
      @Html.Action("two" , "Home")
      @Html.Action("three" , "Home")
    .
    .
    .

i have 5 table in data base.and some of them have relation.some fields of these table should be filled in partial ONE,some should be filled in partial TWO,and ... i make a class of combination this 5 table.

    public class ViewModelX
    {


    public Nullable<long> id_sport { get; set; }
    public Nullable<long> id_city { get; set; }
    public Nullable<long> id_spend { get; set; }
    public Nullable<long> id_profile { get; set; }
    public Nullable<int> cost { get; set; }
    public Nullable<long> idFactor { get; set; }
    public Nullable<long> Idpage { get; set; }

    public string username { get; set; }
    public string Namestar { get; set; }
    public string Lnamestar { get; set; }
    public string Tell { get; set; }
    public string cell { get; set; }
    public string Address { get; set; }
    public string Code { get; set; }



    public string username_s { get; set; }
    public Nullable<long> id_s { get; set; }
    public Nullable<long> id_mark{ get; set; }

     }

now i should pass this model to every partial? and i should pass it to my basic page that contains this 3 partial views too?

Upvotes: 0

Views: 2272

Answers (2)

Ali reza Soleimani Asl
Ali reza Soleimani Asl

Reputation: 167

I had your problem before . If your partial view should get some data use @Html.RenderAction() like below:

Your Partial view (_theLastPost.cshtml) :

@model IEnumerable<test1.Models.Post>

    @foreach (var item in Model)
    {
        <h2>
            @item.Title
        </h2>
        <p>
            @item.Content
        </p>
    }

In your Index.cshtml embed partial view :

@{ Html.RenderAction("_theLastPost"); }

And you should have a controller with same name of partial view like below :

public PartialViewResult _theLastPost()
    {
        var a = (from c in db.Posts
                 orderby c.ID_Post descending
                 select c);
        return PartialView(a.ToList());
    }

Upvotes: 0

Chamaququm
Chamaququm

Reputation: 6728

  1. You should not Pass the Model(associated with your Business Layer) to you View.

  2. Your opinion might comes simply from a desire to write/maintain less code. So, It is obvious why creating new view models all of the time will lead to more code.

  3. Reasons to Use a View Model when it makes sense (and this has happened to me), this allows you to validate your view model differently than your model for attribute-based validation scenarios.

  4. View model objects can be used to help shape and format data. Need a date or money value formatted a particular way? You can do that in the view, in the controller, or in the view model. If all you are doing is formatting and such, you can make a case that the view model is the best place to do it.

  5. By using a view model you have a good mechanism to flatten out and simplify what the view deals with. This will also filter down what can be seen in intellisense, so if you have different people developing the models than those working on the views, creating a simple view model can make it much easier for those just dealing with the UI.

Conslusion : View Model should contain only those properties that are required for it's corresponding View.

Reference

View Models

public class PartialViewModel1
{
    [Display(Name = "Name")]
    public String Name { get; set; }
}

public class PartialViewModel2
{
    [Display(Name = "id")]
    public int id { get; set; }
}

public class PartialViewModel3
{
    [Display(Name = "DOB")]
    public DateTime DOB { get; set; }
}

Controller Action Methods

[HttpGet]
public PartialViewResult PartialView1()
{
    return PartialView(new PartialViewModel1());
}

[HttpGet]
public PartialViewResult PartialView2()
{
    return PartialView(new PartialViewModel2());
}

[HttpGet]
public PartialViewResult PartialView3()
{
    return PartialView(new PartialViewModel3());
}

Partial Views - 1

@model Practise.Controllers.PartialViewModel1
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.EditorForModel();
    <input type="submit" name="Submit" value="Submit" />
}

Partial Views - 2

@model Practise.Controllers.PartialViewModel2
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.EditorForModel();
    <input type="submit" name="Submit" value="Submit" />
}

Partial Views - 3

@model Practise.Controllers.PartialViewModel3
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.EditorForModel();
    <input type="submit" name="Submit" value="Submit" />
}

View

@Html.Action("PartialView1", "Account", new { area = "AreaName" })
@Html.Action("PartialView2", "Account", new { area = "AreaName" })
@Html.Action("PartialView3", "Account", new { area = "AreaName" })

Upvotes: 2

Related Questions