如何将现有视图与自己的控制器作为局部视图添加到另一个视图?

时间:2015-12-17 13:35:34

标签: asp.net-mvc asp.net-mvc-4 visual-studio-2013 partial-views

我正在使用visual studio 2013社区版。在我的ASP.NET MVC应用程序中,我有一个模型和控制器视图如下。我从scafolding创建了视图。这是数据库第一种方法。我添加了edmx文件,然后添加了带scafolding的控制器,它创建了视图。

型号:

using System;
using System.Collections.Generic;

public partial class request
{
    public request()
    {
        this.stocks = new HashSet<stock>();
        this.transactions = new HashSet<transaction>();
    }

    public int request_id { get; set; }
    public Nullable<long> request_no { get; set; }
    public int request_status { get; set; }

    public virtual ICollection<stock> stocks { get; set; }
    public virtual ICollection<transaction> transactions { get; set; }
}

查看:

@model MaterialManagement2.request

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>request</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.request_no, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.request_no, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.request_no, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.request_status, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.request_status, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.request_status, "", 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>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

控制器:

namespace MaterialManagement2.Controllers
{
    public class requestsController : Controller
    {
        private MaterialManagement2Entities db = new MaterialManagement2Entities();

    // GET: requests
    public ActionResult Index()
    {
        return View(db.requests.ToList());
    }

    // GET: requests/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        request request = db.requests.Find(id);
        if (request == null)
        {
            return HttpNotFound();
        }
        return View(request);
    }

    // GET: requests/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: requests/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "request_id,request_no,request_status")] request request)
    {
        if (ModelState.IsValid)
        {
            db.requests.Add(request);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(request);
    }

    // GET: requests/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        request request = db.requests.Find(id);
        if (request == null)
        {
            return HttpNotFound();
        }
        return View(request);
    }

    // POST: requests/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "request_id,request_no,request_status")] request request)
    {
        if (ModelState.IsValid)
        {
            db.Entry(request).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(request);
    }

    // GET: requests/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        request request = db.requests.Find(id);
        if (request == null)
        {
            return HttpNotFound();
        }
        return View(request);
    }

    // POST: requests/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        request request = db.requests.Find(id);
        db.requests.Remove(request);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

现在我需要将此视图呈现为具有另一个具有单独控制器和模型的视图的部分视图。我用创建模板创建了另一个带有上述模型的视图(部分)。这个局部视图称为&#34; _request&#34;驻留在共享文件夹中。现在我用

@Html.Partial("_request", new MaterialManagement2.request())

在我的另一个视图中所以我可以使用它们都是单个视图但是具有局部视图的新视图的创建按钮不起作用。如何使该按钮有效?

以下是该应用的屏幕截图。视图呈现正确,看起来就像我想要的那样。该按钮不起作用。

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须在此行中指定第一个控制器名称:

if let n = arr[1] {
    // how to see the difference if my index is out of range
    // or i receive valid nil value?
}

你应该在提交按钮中删除它:

@using (Html.BeginForm("Create", "requestsController"))