结合创建视图和索引视图c#MVC5

时间:2016-11-30 14:49:24

标签: c# asp.net-mvc

我尝试合并用于创建通知的视图(create.cshtml)和用于列出已创建的通知(index.cshtml)的视图。我已经尝试了here所列的选项,但都没有成功。我知道@model IEnumerable<BhSpyGlass.Models.NotificationType>@model BhSpyGlass.Models.NotificationType之间存在冲突,但我不确定如何解决它。这是我的代码:

控制器:

private BhSpyGlass_dbEntities db = new BhSpyGlass_dbEntities();

public ActionResult Index()
{
    var notificationTypes = db.NotificationType.Include(n => n.RecipientType);
    return View(notificationTypes.ToList());
}

public ActionResult Create()
{
    ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole");
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,notificationType1,recipientTypeId")] NotificationType notificationType)
{
    if (ModelState.IsValid)
    {
        db.NotificationType.Add(notificationType);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole", notificationType.recipientTypeId);
    return View(notificationType);
}

查看Index.cshtml:

@model IEnumerable<BhSpyGlass.Models.NotificationType>
....
@Html.Partial("_PartialNTCreate",new _Page_Views_NotificationTypes_Index_cshtml())
<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.notificationType1)</th>
        <th>@Html.DisplayNameFor(model => model.RecipientType.recipientRole</th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>@Html.Partial("_PartialNTList", item)</tr>
    }

</table>

查看Create.cshtml:

@model BhSpyGlass.Models.NotificationType
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.notificationType1, "Notification Name", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.notificationType1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.notificationType1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.recipientTypeId, "Who will receive this notification?", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("recipientTypeId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.recipientTypeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

_PartialNTList.cshtml

@model BhSpyGlass.Models.NotificationType
<tr id="listed">
    <td>@Html.DisplayFor(model => model.notificationType1)</td>
    <td>@Html.DisplayFor(model => model.RecipientType.recipientRole)</td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
        @Html.ActionLink("Details", "Details", new { id = Model.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id = Model.Id })
     </td>
</tr>

0 个答案:

没有答案