如何在单一视图创建和显示模型数据? [英] How to create and display model data on a single view?

查看:98
本文介绍了如何在单一视图创建和显示模型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的目标是能够从相同的视图中的一个模型创建和显示无序列表项

The goal is to be able to create and display an unordered list item from a model within the same view.

这是我到目前为止结果
型号
Notes.cs

This is what I have so far
Model: Notes.cs

namespace NoteTaking.Models
{
    public class Notes
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public float Time { get; set; }
    }

    public class MyDbContext : DbContext
    {
        public MyDbContext() : base("Notes")
        {

        }

        public DbSet<Notes> Notes { get; set; }
    }
}

HomeController.cs

HomeController.cs

namespace NoteTaking.Controllers
{
    public class HomeController : Controller
    {
        MyDbContext db = new MyDbContext();
        // GET: Home
        public ActionResult Index()
        {
            return View(db.Notes.ToList());
        }

        // POST: Home
        [HttpPost]
        public ActionResult Index([Bind(Include = "Id,Text,Time")]Notes notes)
        {
            if (ModelState.IsValid)
            {
                db.Notes.Add(notes);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View();
        }
    }
}

Index.cshtml

Index.cshtml

@model NoteTaking.Models.Notes

@{
    ViewBag.Title = "Index";
}

<div class="jumbotron"></div>

@using (Html.BeginForm())
{
    @Html.EditorFor(model => model.Text)
    <input type="submit" value="Create" class="btn btn-default" />
}

<div>
    <h2>Notes</h2>
    @foreach (var d in Model.Text)
    {
        <ul>
            <li>
                @d
            </li>
        </ul>
    }
</div>

我知道我会得到一个编译错误,我试图做的事情很多喜欢使用的IEnumerable来解决这个问题,但是,这并不让我使用@ Html.EditorFor。

I understand that I will get a compiling error and I have tried to fix this by doing numerous things like using IEnumerable but this doesn't allow me to use the @Html.EditorFor.

我很困惑。

推荐答案

您指数方法返回集合票据

Your Index method is returning a collection of notes

return View(db.Notes.ToList());

这样的观点应该是

so the view should be

@model IEnumerable<NoteTaking.Models.Notes>
@using (Html.BeginForm())
{
  @Html.EditorFor(m => m)
  <input type="submit" value="Create" class="btn btn-default" />
}

这将显示所有注释。默认情况下,这将创建的所有笔记3个属性的输入。您可以创建一个自定义的 EditorTemplate 名为 Notes.cshtml 查看/共享/ EditorTemplates 自定义您的显示器

which will display all Notes. By default this will create inputs for all 3 properties of notes. You can create a custom EditorTemplate named Notes.cshtml in Views/Shared/EditorTemplates to customise your display

@model NoteTaking.Models.Notes
@Html.HiddenFor(m => m.ID)
@Html.TextBoxFor(m => m.Text)
@Html.HiddenFor(m => m.Time)

和您的POST方法应该是

and your POST method should be

public ActionResult Index(IEnumerable<Notes> notes)

因为你的编辑集合票据。

since your editing a collection of notes.

注:没有多少点也显示在无序列表的收集您后来的所有音符都已经在使用显示完成 EditorFor

Note: There is not much point also displaying the collection in the unordered list as you have done since all notes are already displayed using EditorFor

修改

根据您的要求修改你需要包含要显示和性能视图模型修改

Based on your revised requirements you need a view model that contains the properties you want to display and edit

public class NotesVM
{
  public List<Notes> NoteList { get; set; }
  public Notes NewNote { get; set; }
}

和在控制器

public ActionResult Index()
{
  NotesVM model = new NotesVM();
  model.NewNote = new Notes();
  model.NoteList = db.Notes.ToList();
  return View(model);
}

和视图

@model NotesVM
....
@using (Html.BeginForm())
{
  @Html.EditorFor(m => m.NewNote.Text)
  <input type="submit" value="Create" class="btn btn-default" />
}
....
<ul>
  @foreach (var d in Model.NotesList)
  {
    <li>@d.Text</li>
  }
</ul>

和在岗方法

[HttpPost]
public ActionResult Index([Bind(Prefix="NewNote")]Notes model)
{
  // Note you only have a control for `Test` so `ID` and `Time` will both be default values
  if (ModelState.IsValid)
  {
    db.Notes.Add(model);
    db.SaveChanges();
    return RedirectToAction("Index");
  }
  return View(model);
}

请注意我还建议创建类视图模型注释本身,因此您可以添加验证属性。

Note I would also recommend creating a view model for class Notes itself so you can add validation attributes

这篇关于如何在单一视图创建和显示模型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆