我的桌子后面有几列
Id,Name,A_Status,BranchName.
用下面的方法,我会这样说来达到预期效果吗? 列分支有25个不同的名称。因此,我想创建一个包含25个分支标题的页面(每个标题都创建自己的cshtml页面),用户可以在其中选择并输入该特定分支的数据。在这些页面的每个页面上,并非所有用户都可以输入和查看特定的权限。 我决定在下一个代码中实现它。
我的控制器
public ActionResult A_Branch ()
{
string query = "SELECT BranchName,Name, MAX(A_STATUS) AS A_Status "
+ "FROM Students "
+ "WHERE BranchName= '1_Branch' "
+ "GROUP BY BranchName,Name";
IEnumerable<EnrollmentStudentGroup> data = db.Database.SqlQuery<EnrollmentStudentGroup>(query);
return View(data.ToList());
}
下一个要创建的动作结果将增加23倍。
public ActionResult B_Branch ()
{
string query = "SELECT BranchName,Name, MAX(A_STATUS) AS A_Status "
+ "FROM Students "
+ "WHERE BranchName= '2_Branch' "
+ "GROUP BY BranchName,Name";
IEnumerable<EnrollmentStudentGroup> data = db.Database.SqlQuery<EnrollmentStudentGroup>(query);
return View(data.ToList());
}
我的2个模型;
public class Student : BaseEntity
{
public int Id { get; set; }
public string BranchName { get; set; }
public string Name { get; set; }
public int A_Status { get; set; }
}
public class EnrollmentStudentGroup
{
public string BranchName { get; set; }
public string Name { get; set; }
public int A_Status { get; set; }
public IEnumerable<EnrollmentStudentGroup> StudentCollection { get; set; }
}
和我的模特
public class Model
{
public List<Student> Students { get; set; }
public List<EnrollmentStudentGroup> EnrollmentStudentGroup { get; set; }
}
我最新的控制器(在“ .select”之后,他给了我错误)
[HttpGet]
public ActionResult TestNew(string branchname)
{
// check stuff like permissions
var db = new MovieContext();
var model = new Model();
var students = db.Student
.Where(x => x.BranchName == branchname)
.GroupBy(x => new { x.BranchName, x.Name, x.Currency, x.NoCart, x.NoAccount })
.Select(x => new
{
BranchName = x.FirstOrDefault().BranchName,
Name = x.FirstOrDefault().Name,
A_Status = x.Max(p => p.A_Status),
Currency = x.FirstOrDefault().Currency,
NoCart = x.FirstOrDefault().NoCart,
NoAccount = x.FirstOrDefault().NoAccount
}).ToList();
foreach (var item in students)
{
model.Students.Add(new Student
{
A_Status = item.A_Status,
BranchName = item.BranchName,
Name = item.Name,
NoAccount = item.NoAccount,
NoCart = item.NoCart,
Currency = item.Currency
});
}
return View(model);
}
现在出现该错误,我根据您的回答更新了我的模型和视图(cshtml)。
我的观点(cshtml)
@model Tessa.Models.Model
@{
ViewBag.Title = "BranchView";
}
<h2>BranchView</h2>
@foreach (var item in Model.Students)
{
<p>@item.Name</p>
<p>@item.A_Status</p>
<p>@item.BranchName</p>
<p>@item.NoAccount</p>
<p>@item.NoCart</p>
<p>@item.Currency</p>
}
答案 0 :(得分:2)
正如评论中所写,您不必对每个分支都执行此操作。这是一种可以帮助您的解决方案
您的控制器
public ActionResult Branch(string branchname)
{
var db = new ApplicationDbContext();
var model = new Model();
var students = db.Students
.Where(x => x.BranchName == branchname)
.GroupBy(x => new { x.BranchName, x.Name, x.Currency, x.NoCart, x.NoAccount })
.Select(x => new
{
BranchName = x.FirstOrDefault().BranchName,
Name = x.FirstOrDefault().Name,
A_Status = x.Max(p => p.A_Status),
Currency = x.FirstOrDefault().Currency,
NoCart = x.FirstOrDefault().NoCart,
NoAccount = x.FirstOrDefault().NoAccount
}).ToList();
foreach (var item in students)
{
model.Students.Add(new Student
{
A_Status = item.A_Status,
BranchName = item.BranchName,
Name = item.Name,
NoAccount = item.NoAccount,
NoCart = item.NoCart,
Currency = item.Currency
});
}
return View(model);
}
因此,您仅将分支名称传递给控制器。她是执行此操作的几种不同方式。
将模型传递给视图是一种很好的做法。
public class Model
{
public List<Student> Students { get; set; }
//Some other propertys
public Model()
{
Students = new List<Student>();
}
}
您可以每隔几年这样写您的观点
@model TestApp.Models.Model
@{
ViewBag.Title = "BranchView";
}
<h2>BranchView</h2>
@foreach (var item in Model.Students)
{
<p>@item.Name</p>
<p>@item.A_Status</p>
<p>@item.Branchname</p>
<p>@item.NoAccount</p>
<p>@item.NoCart</p>
<p>@item.Currency</p>
}
在foreach
循环中,您可以执行常规的html操作。这将对列表中的每个元素完成。
希望对您有帮助。