如何在实体框架生成表单后更改表单

本文关键字:表单 实体 框架 | 更新日期: 2023-09-27 18:31:28

请不要太用力地争论我,我只是 asp.net 的初学者

我已经基于此模型制作了控制器/视图 http://tinypic.com/view.php?pic=awq14w&s=8#.VG2Q4PmUdUV

当我创建一个新的调查时,我有这个视图 http://tinypic.com/view.php?pic=33ayubs&s=8#.VG2R6fmUdUV

如何在创建/查看/编辑时修改视图以获取此表单类型:

Questions | Answers | Comments
------------------------------
Q1        | TextBox | TextBox
Q2        | TextBox | TextBox
Q3        | TextBox | TextBox

控制器:

    public class SurveysController : Controller
    {
        private OrganizationASEMEntities1 db = new OrganizationASEMEntities1();
        // GET: Surveys
        public ActionResult Index()
        {
            var surveys = db.Surveys.Include(s => s.Question).Include(s => s.User);
            return View(surveys.ToList());
        }
        // GET: Surveys/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Survey survey = db.Surveys.Find(id);
            if (survey == null)
            {
                return HttpNotFound();
            }
            return View(survey);
        }
        // GET: Surveys/Create
            public ActionResult Create()
            {
                ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText");
                ViewBag.UserId = new SelectList(db.Users, "UserId", "Name");
                return View();
            }
            // POST: Surveys/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 = "SurveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
            {
                if (ModelState.IsValid)
                {
                    db.Surveys.Add(survey);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
                ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
                return View(survey);
            }
            // GET: Surveys/Edit/5
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Survey survey = db.Surveys.Find(id);
                if (survey == null)
                {
                    return HttpNotFound();
                }
                ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
                ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
                return View(survey);
            }
            // POST: Surveys/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 = "SurveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(survey).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
                ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
                return View(survey);
            }
            // GET: Surveys/Delete/5
            public ActionResult Delete(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Survey survey = db.Surveys.Find(id);
                if (survey == null)
                {
                    return HttpNotFound();
                }
                return View(survey);
            }
            // POST: Surveys/Delete/5
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            public ActionResult DeleteConfirmed(int id)
            {
                Survey survey = db.Surveys.Find(id);
                db.Surveys.Remove(survey);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }

查看Create

    @model OrganizationASEM.Models.Survey
    @{
        ViewBag.Title = "Create";
    }
    <h2>Create</h2>
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <h4>Survey</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.QuestionId, "QuestionId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("QuestionId", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.QuestionId, "", new { @class = "text-danger" })
                </div>
            </div>
        enter code here
            <div class="form-group">
                @Html.LabelFor(model => model.Answer, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Answer, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Answer, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Comment, "", 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>

查看Edit

@model OrganizationASEM.Models.Survey
@{
    ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Survey</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.SurveyId)
        <div class="form-group">
            @Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.QuestionId, "QuestionId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("QuestionId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.QuestionId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Answer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Answer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Answer, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Comment, "", 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>

查看Index

@model IEnumerable<OrganizationASEM.Models.Survey>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Answer)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comment)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Question.QuestionText)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.User.Name)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Answer)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Comment)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Question.QuestionText)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.SurveyId }) |
            @Html.ActionLink("Details", "Details", new { id=item.SurveyId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.SurveyId })
        </td>
    </tr>
}
</table>

如何在实体框架生成表单后更改表单

如果你想

把所有 fieds 放在一行中,你必须从:

<div class="form-horizontal">

自:

<div class="form-inline">

看这里:

http://getbootstrap.com/css/#forms