Asp.net mvc MVC Html.EditorFor textbox不呈现
Edit.cshtmlAsp.net mvc MVC Html.EditorFor textbox不呈现,asp.net-mvc,Asp.net Mvc,Edit.cshtml @model MVCExercise.Models.User @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Edit</h2> <div> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-
@model MVCExercise.Models.User
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
<div>
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div>
@Html.EditorFor(model => model.ID, new { htmlAttributes = new { @class = "form-control" } })
</div>
HomeController.cs
public class HomeController : Controller
{
[HttpPost]
public ActionResult Edit(User usr)
{
var id = usr.ID;
var name = usr.Name;
//update database here..
return RedirectToAction("Index");
}
public ActionResult Edit(string Id)
{
//Get the student from studentList sample collection for demo purpose.
//Get the student from the database in the real application
var std = users.Where(s => s.ID == Id).FirstOrDefault();
return View(std);
}
}
表中显示的数据:
在表的第一个条目上单击Edit之后,我得到下图。文本框中仅呈现model.ID
Model.name文本框完全为空:
编辑.cshtml:
@model MVCExercise.Models.User
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
UsersController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCExercise.Models
{
public class User
{
public string Greeting => "Hello User";
public string Name { get; set; }
public string ID { get; set; }
}
}
// POST: Users/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
表格中显示的数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCExercise.Models
{
public class User
{
public string Greeting => "Hello User";
public string Name { get; set; }
public string ID { get; set; }
}
}
// POST: Users/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
单击表格条目上的编辑后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCExercise.Models
{
public class User
{
public string Greeting => "Hello User";
public string Name { get; set; }
public string ID { get; set; }
}
}
// POST: Users/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
您确定std中填充了名称吗?实际上,这可能是问题所在。