MVC Html.EditorFor текстовое поле не отображает - PullRequest
0 голосов
/ 25 октября 2018

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-control" } })
</div>
<div>
    @Html.EditorFor(model => model.ID, new { htmlAttributes = new { @class = "form-control" } })
</div>

TestModel.cs

namespace MVCExercise.Models
{
    public class User
    {
        public string Greeting => "Hello User";
        public string Name { get; set; }

        public string ID { get; set; }
    }
}

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);
    }
  }

Данные, отображаемые в таблице:

enter image description here

После нажатия «Изменить» в первой записи таблицы я получаю следующее изображение.Только модель .ID отображается в текстовом поле.

Текстовое поле Model.name полностью пустое:

enter image description here

1 Ответ

0 голосов
/ 26 октября 2018

Edit.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")
}

TestModel.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; }
    }
}

UsersController.cs:

// 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);
        }

Данные, отображаемые в таблице:

enter image description here

После нажатия Изменить назапись в таблице:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...