VS 2012 MVC - 使用一个视图&更新和创建功能的模型

时间:2013-08-23 01:20:28

标签: c# asp.net-mvc asp.net-mvc-4 visual-studio-2012

我是C#和MVC的新手,我将从PHP背景进入Visual Studio环境。我正在摸索一下(很多),但到了那里。我需要帮助来理解一些基本概念,毫无疑问,这是一个非常混乱的努力,但帮助我解决这个问题将有助于我了解事情的运作方式。

我有一个SQL数据库,通过添加ADO.NET实体类模型连接到VS2012 Web项目(C#)。我只想说那个结果没问题,自动生成的默认CRUD视图也没问题。

使用案例

我想要一个网页,向用户显示从数据库中提取的项目列表。这已经在默认的索引视图中实现,但我还希望该列表可以编辑,而无需为每个单独的项目输入编辑视图。因此,我将列表HTML更改为表单,并为“更新”提交设置ActionName。这也没有问题:

MODEL:

namespace Library3.DAL {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class PrmTbl_Level {
        public PrmTbl_Level() {
            this.Tbl_Books = new HashSet<Tbl_Book>();
        }

        [Required]
        [Display(Name = "ID")]
        public int LevelID { get; set; }

        [Required]
        [Display(Name = "Description")]
        public string LevelDesc { get; set; }

        [Required]
        [Display(Name = "Entered By")]
        public string EnteredBy { get; set; }

        public Boolean Active { get; set; }

        public virtual ICollection<Tbl_Book> Tbl_Books { get; set; }
    }
}

查看:

@model IEnumerable<Library3.DAL.PrmTbl_Level>


@using (Html.BeginForm("Update", "ReaderLevel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <table>
        <thead>
            <tr>
                <th> @Html.DisplayNameFor(model => model.LevelID) </th>
                <th> @Html.DisplayNameFor(model => model.LevelDesc) </th>
                <th> @Html.DisplayNameFor(model => model.EnteredBy) </th>
                <th> @Html.DisplayNameFor(model => model.Active) </th>
            </tr>
        </thead>

        @foreach (var item in Model) {

            var thisID = item.LevelID;
            var thisDesc = item.LevelDesc;
            var thisEnteredBy = item.EnteredBy;
            var thisActive = item.Active;

            <tr>
                <td class="tdsm centered fade"> @thisID </td>
                <td> <input type="text" name="@(thisID + ".desc")" value="@thisDesc" /> </td>
                <td class="tdmed centered fade"> @thisEnteredBy </td>
                <td class="tdsm centered">
                    @{
                        if(@thisActive) {
                            <text><input type="checkbox" name="@(thisID + ".active")" value="true" checked="checked" /></text>
                        }
                        else {
                            <text><input type="checkbox" name="@(thisID + ".active")" value="true" /></text>
                        }
                    }
                </td>
            </tr>
        }all of this
    </table>
    <p>
        <input type="submit" value="Update" />
    </p>
</fieldset>
}

控制器:

namespace Library3.Controllers {
    public class ReaderLevelController : Controller {
        private NSSchoolLibraryEntities db = new NSSchoolLibraryEntities();

        public ActionResult Index() {
            ViewBag.Title = "Reader Levels";
            return View(db.PrmTbl_Levels.ToList());
        } 


        [HttpPost, ActionName("Update")]
        [ValidateAntiForgeryToken]
        public ActionResult Update(FormCollection MyArray) {

                (... Process UPDATE submission ...)

                db.SaveChanges();

            }
        }

        return RedirectToAction("Index");

        }
    }
}

现在我想在列表的顶部放置一个空字段,从而可以创建新条目。这基本上意味着一个视图文件现在将适应创建和更新功能。我插入了以下HTML:

@using (Html.BeginForm("New", "ReaderLevel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>

        <table>
            <tr>
                <td class="tdsm centered fade">
                    @Html.EditorFor(model => model.LevelID)
                    @Html.ValidationMessageFor(model => model.LevelID)</td>
                <td><input type="text" name="desc" />
                    @Html.EditorFor(model => model.LevelDesc)
                    @Html.ValidationMessageFor(model => model.LevelDesc)</td>
                <td class="tdmed centered fade">
                    @Html.EditorFor(model => model.EnteredBy)
                    @Html.ValidationMessageFor(model => model.EnteredBy)</td>
                <td class="tdsm centered">
                    @Html.EditorFor(model => model.Active)
                    @Html.ValidationMessageFor(model => model.Active)</td>
            </tr>
        </table>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

...这里引用的模型的问题应该是PrmTbl_Level类的新内容 - 不使用现有的IEnumberable来从表中吐出现有数据(至少这就是我认为问题所在! )。

默认的单独的Create视图通过@model引用命名空间(没有IEnumberable),数据作为单个对象(public ActionResult Create(PrmTbl_Level prmtbl_level))返回,然后毫不费力地保存到db({{1 }} - 如何使用一个db.PrmTbl_Levels.Add(prmtbl_level);语句实现相同的功能? (ELI5)。所有帮助 - 包括实际和所涉及的概念 - 非常感谢!

2 个答案:

答案 0 :(得分:2)

简而言之,您不应该为模型使用IEnumerable<Library3.DAL.PrmTbl_Level>,而应创建一个自定义类来表示列表(显示和编辑当前的内容)以及另外一个PrmTbl_Level。填充以创建新的实体/行。

所以,像这样:

public class MyCustomModel
{
    public MyCustomModel()
    {
        MyList = new List<PrmTbl_Level>();
        MyItemToCreate = new PrmTbl_Level();
    }

    public List<PrmTbl_Level> MyList { get; set; }
    public PrmTbl_Level MyItemToCreate { get; set; }
}

现在您的强类型模型是MyCustomModel,您需要更改视图才能正确引用新模型(例如model => model.LevelID变为model => model.MyList.LevelID)。

不是一个完整的解决方案,但应该让你开始。

答案 1 :(得分:0)

在同一列表视图中添加“创建”视图您可以使用局部视图。并在列表视图顶部添加此部分视图,以解决您的问题。创建新条目所需的所有字段都应在部分视图中。

check these link may be it could give you some help

check these link may be it could give you some help