如何在 ModelView 中添加另一个值并从我的 Action 中传递它
How can I add another value in ModelView and pass it from my Action
在我的产品部分视图中 table
@model IEnumerable<Products.Models.ProductModelView>
如果用户 departmentId 与产品 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>
</td>
}
</tr>
}
这是我的 Class,我正在尝试创建但不知道如何传递到我的 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?
}
谁能帮我制定 Class、ViewModel 和 Action?
创建一个 "composite" 对象并传递它(根据@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;
在我的产品部分视图中 table
@model IEnumerable<Products.Models.ProductModelView>
如果用户 departmentId 与产品 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>
</td>
}
</tr>
}
这是我的 Class,我正在尝试创建但不知道如何传递到我的 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?
}
谁能帮我制定 Class、ViewModel 和 Action?
创建一个 "composite" 对象并传递它(根据@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;