在MVC中提交时,以可读格式显示提交的表单?

时间:2016-03-11 15:25:20

标签: c# asp.net-mvc

我试图找出在用户在MVC中提交表单后如何以只读方式显示表单。因此,当用户点击创建时,应将其带到另一个视图,仅显示他们输入的内容。我还想允许用户返回表单并根据需要进行编辑。

以下是付款控制器的代码......

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,NameOnCard,CardNumber,ValidFrom,Expires,SecurityCode,Address,TownCity,Country,PostCode")] Payment payment)
    {
        if (ModelState.IsValid)
        {
           db.Payments.Add(payment);
           db.SaveChanges();


        }
        return View(payment);

    }

以下是付款视图的视图......

@model charity.Models.Payment

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Payment</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.NameOnCard, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.NameOnCard, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.NameOnCard, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CardNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CardNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CardNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ValidFrom, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ValidFrom, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ValidFrom, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Expires, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Expires, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Expires, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SecurityCode, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SecurityCode, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.SecurityCode, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.TownCity, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.TownCity, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.TownCity, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PostCode, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PostCode, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PostCode, "", 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>

1 个答案:

答案 0 :(得分:2)

在您的控制器中,在SaveChanges后重定向到Details操作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,NameOnCard,CardNumber,ValidFrom,Expires,SecurityCode,Address,TownCity,Country,PostCode")] Payment payment)
{
    if (ModelState.IsValid)
    {
        db.Payments.Add(payment);
        db.SaveChanges();

        return RedirectToAction("Details", new { id = payment.ID);

    }
    return View(payment);

}

public ActionResult Details(int? id)
{
  if (id == null)
  {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  }
  Payments payment = db.Payments.Find(id);
  if (payment == null)
  {
      return HttpNotFound();
  }
  return View(payment);
}

(假设您已有编辑和索引视图)创建详细信息视图,如下所示:

@model charity.Models.Payment

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Payment</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.NameOnCard)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.NameOnCard)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.CardNumber)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.CardNumber)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ValidFrom)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ValidFrom)
        </dd>

        @* Add more fields *@
    </dl>
</div>

<div>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</div>