我对MVC很陌生,并且仍然熟悉MVC的工作原理。基本上,我的用户 model
有一个创建 view
。我使用Razor
语法从用户 model
获取变量:
Create.cshtml
@model CDS.Models.UserModels
@{
ViewBag.Title = "Create User";
}
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.firstname)
@Html.TextBoxFor(m => m.firstname)
<input type="submit" id="btnSave" value="Save" class="btn btn-default" />
}
UserModels.cs
namespace CDS.Models
{
public class UserModels
{
public string userid { get; set; }
[Display(Name = "First Name")]
public string firstname{ get; set; }
public IEnumerable<SelectListItem> filteroptions { get; set; }
}
}
我尝试从控制器的view
方法自动生成索引 Index
以列出数据库记录,但发现生成的索引 view
正在使用Database
model
(第一行代码)。我只想将代码从Index.cshtml移动到我的Create.cshtml,让后者View
也显示数据库记录。那我该怎么做呢?我听说我需要使用Javascript吗?
UserController.cs
namespace CDS.Controllers
{
public class UserController : Controller
{
CDSEntities _odb = new CDSEntities(); //My Database
// GET: User
public ActionResult Index()
{
return View(_odb.USR_MSTR.ToList());
}
// GET: User/Create
public ActionResult Create()
{
var filters = GetAllFilters();
var model = new UserModels();
model.filteroptions = GetSelectListItems(filters);
return View(model);
}
}
}
Index.cshtml
@model IEnumerable<CDS.USR_MSTR>
@{
ViewBag.Title = "Index";
}
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.FIRST_NM)</th>
<th>@Html.DisplayNameFor(model => model.LAST_NM)</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.FIRST_NM)</td>
<td>@Html.DisplayFor(modelItem => item.LAST_NM)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
请注意,我删除了我认为没有必要在此处发布的代码。这些只是我的类和HTML的摘要