如何在ModelView中添加其他值并从我的Action

时间:2017-09-10 15:02:30

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

在我的产品PartiallView表中

@model IEnumerable<Products.Models.ProductModelView>

如果用户departmentId与product departmentId不同,我正在尝试(隐藏)不显示某些按钮

<table class="table table-responsive table-hover table-striped">
    <tr>
        <th>Product type</th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>

            //  I want to hide if current inlogged userDepartmentId is not the same as product departmentId
            // Like @if(@item.ProductDeptId == userDepartmentId) {
            <td>
                <Button class="btn btn-success glyphicon btn-xs glyphicon-plus" onclick="return EditDelete(@(item.ProductId) )">Edit/Delete</Button>&nbsp;
            </td>
            }
        </tr>
    }

这是我试图创建但我不知道如何传递给我的PartielView的类,ViewModel和Action

 public class Product
 {
     public int ProductId { get; set; }
     public string productName { get; set; }
     public int DepartmentId { get; set; }
 }

public class ProductModelView
{
    public int ProductId { get; set; }
    public string productName { get; set; }
    public int DepartmentId { get; set; }
    public int UserDepartmentId { get; set; }
}

[HttpGet]
public ActionResult Lager()
{
    using (context)
    {
        string user = User.Identity.Name;
        int deptId = context.Users.Where(u => u.UserName == user).Select(d => d.DepartmentId).SingleOrDefault();

        // Here I don't know how to continue ... I try like this
        Product pr = new Product();
        ProductModelView prModel = new ProductModelView();
        prModel.ProductId = pr.ProductId;
        prModel.productName  = pr.productName ;
        prModel.DepartmentId= pr.DepartmentId;
        prModel.UserDepartmentId = DeptId;

        // And then, how to return and what to return?

        return PartialView("_ProductList",  prModel); // Is this right? Returning prModel?
    }

任何人都可以帮我制定我的班级,ViewModel和我的行动吗?

1 个答案:

答案 0 :(得分:0)

创建&#34;复合&#34;对象并传递它(根据@Uwe):

public class ProductListViewModel
{
    public IEnumerable<ProductModelView> Products { get; set; }
}

并相应地更改@model

@model ProductListViewModel

构建并将IEnumerable<ProductModelView> products传递给return PertialView()

// IQueryable<Product>
var products = from p in context.Products
               where p.DepartmentId == depId // aware! would cause 2 queries to db, shall be rewritten
               select p;
var prModels = from p in products.AsEnumerable() // 'memorize' db query into in-memory objects
               select new ProductViewModel
               {
                   ProductId = p.ProductId,
                   ProductName = p.productName,
                   DepartmentId = p.DepartmentId;
                   UserDepartmentId = deptId;
               };
return return PartialView("_ProductList", prModels);

如何重写:

配置导航属性(推荐):

public class Product
{
    public User { get; set; }
}

执行直接加入:

var products = from p in context.Products
               join u in context.User on p.UserDepartmentId = u.UserDepartmentId // or whatever it is
               where u.UserName == userName // it's better to use userId which is likely the primary key
               select p;