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

C# 实体框架生成表单后如何更改表单,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,请不要跟我争论太多,我只是asp.net的初学者 我已经基于此模型制作了控制器/视图 创建新调查时,我有以下视图 在创建/查看/编辑时,如何修改视图以获取此表单类型: Questions | Answers | Comments ------------------------------ Q1 | TextBox | TextBox Q2 | TextBox | TextBox Q3 | TextBox | TextBox 控制器: publ

请不要跟我争论太多,我只是asp.net的初学者

我已经基于此模型制作了控制器/视图

创建新调查时,我有以下视图

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

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);
            }
        }
    }
查看
创建

    @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>
查看
索引

@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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.Answer) @DisplayNameFor(model=>model.Comment) @DisplayNameFor(model=>model.Question.QuestionText) @DisplayNameFor(model=>model.User.Name) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.Answer) @DisplayFor(modelItem=>item.Comment) @DisplayFor(modelItem=>item.Question.QuestionText) @DisplayFor(modelItem=>item.User.Name) @ActionLink(“编辑”,“编辑”,新的{id=item.SurveyId})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.SurveyId})| @ActionLink(“删除”,“删除”,新的{id=item.SurveyId}) }
如果您想将所有场都放在一行中,您必须从以下位置切换:

<div class="form-horizontal">

致:


请看这里:


感谢您提供如何设置的建议,但我想知道如何按照我想要的顺序填充db中的数据
<div class="form-horizontal">
<div class="form-inline">