簡體   English   中英

從其他視圖檢索數據

[英]Retrieving data from a different view

我不確定使用session或tempdata是否對這個問題有用,即使我不知道如何執行此操作。 (仍然可以通過asp.net掌握MVC的功能)。

基本上在我的第一個視圖(稱為付款視圖)中,我有很多字段供用戶填寫以捐贈給特定的慈善機構。 現在,為了將其添加到數據庫中並完成捐贈,我將創建一個確認頁面,該頁面將傳輸所有數據並在確認頁面上顯示不可編輯的文本格式,並提供用戶給出的所有信息在我創建的字段中。 在確認頁面上,我希望有一個名為“編輯”的按鈕,該按鈕將允許用戶返回到支付頁面,該頁面中仍存儲了他們插入的數據,但只需單擊輸入字段即可對其進行修改。 。 我認為使用會話或tempdata最適合此操作,但是我很困惑,好像我確實使用了它,當用戶單擊最終確認頁面上的捐贈時,是否將重新啟動網站? 如清除所有保存的數據。 這就是我要的。 我想要一種方法,該方法允許用戶通過單擊其他視圖上的“編輯”來編輯當前存儲的數據,並能夠在付款視圖上對其進行修改。

付款方式

@model CharitySite.Models.Payment
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    ....
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <h1>@ViewBag.Name</h1>
        <label> Full Name:</label>
        <input type="text" id="fullname" name="user_name" />
        <label> Amount:</label>
        <input type="text" id="ammount" name="user_ammount" />
        <label>Comment:</label>
        <input type="text" id="comment" name="user_comment" />
        <br /><br />
        @Html.LabelFor(model => model.CardName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CardName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CardName, "", 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>
    .... // more form controls
    <input type="submit" value="Create" class="btn btn-default" 
}


@Html.ActionLink("Back to List", "Index")
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

支付控制器

public ActionResult Index()
{
    return View(db.Payments.ToList());
}

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

// GET: Payments/Create
public ActionResult Payment()
{
    var model = new Payment();
    model.ValidFrom = DateTime.Now;
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Payment([Bind(Include = "ID,CardName,CardNumber,ValidFrom,Expires,CardSecurityCode,EmailAddress,Address,City,Country,PostCode")] Payment payment)
{
    if (ModelState.IsValid)
    {
        db.Payments.Add(payment);
        db.SaveChanges();
        ViewBag.Name = TempData["Name"];
        ViewBag.Amount = TempData["Amount"];
        ViewBag.Comment = TempData["Comment"];
        return RedirectToAction("Index");
    }
    return View(payment);
}

如果您轉儲所有代碼,我們將無法查明您的問題,因此,我將簡要介紹一些方法。

您可以通過兩種方式進行處理:

  1. 您可以將第一頁中的數據放入查詢字符串中,並從服務器進行重定向。 僅當數據大小可管理且對安全性不敏感時,此方法才有效。

  2. 如果您具有安全敏感的信息或只是為了更好的組織,則可以有一個額外的模型(數據庫中的表)來表示未完成的事務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM