我想从UserProfile表中自动填充AddressAndPayment视图。
我想使用UserProfile表中的字段在AddressAndPayment.cshtml中自动显示。我无法弄清楚如何执行此操作。
某些字段需要来自Billing.cs以及UserProfile表中的其他字段。
感谢。
我想要填充的视图是
AddressAndPayment.cshtml
@model Tp1WebStore3.Models.Billing
@{
ViewBag.Title = "AddressAndPayment";
}
<h2>Billing Info</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Billing</legend>
@Html.HiddenFor(model => model.BillingId)
<div class="editor-label">
@Html.LabelFor(model => model.LastNameBilling)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastNameBilling)
@Html.ValidationMessageFor(model => model.LastNameBilling)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstNameBilling)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstNameBilling)
@Html.ValidationMessageFor(model => model.PrenomFact)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdressBilling)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdressBilling)
@Html.ValidationMessageFor(model => model.AdressBilling)
</div>
以下是我从AccountModels.cs
中存储在UserProfileTable中的字段[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Adress { get; set; }
}
Billing.cs
namespace Tp1WebStore3.Models
{
[Bind(Exclude = "BillingId")]
public partial class Billing
{
[ScaffoldColumn(false)]
public int BillingId { get; set; }
[ScaffoldColumn(false)]
public System.DateTime DateBilling { get; set; }
public string LastNameBilling { get; set; }
public string FirstNameBilling { get; set; }
public string AdresseBilling { get; set; }
CheckoutController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tp1WebStore3.Models;
namespace Tp1WebStore3.Controllers
{
[Authorize]
public class CheckoutController : Controller
{
Tp1WebStoreDBEntities storeDB = new Tp1WebStoreDBEntities();
//
// GET: /Checkout/AddressAndPayment
public ActionResult AddressAndPayment()
{
return View();
}
//
// POST: /Checkout/AddressAndPayment
[HttpPost]
public ActionResult AddressAndPayment(FormCollection values)
{
var billing = new Billing();
TryUpdateModel(billing);
try
{
billing.DateBilling = DateTime.Now;
//Process the order
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.CreateOrder(billing);
return RedirectToAction("Complete",
new { id = billing.BillingId });
}
catch
{
//Invalid - redisplay with errors
return View(billing);
}
}
//
// GET: /Checkout/Complete
public ActionResult Complete(int id)
{
return View(id);
}
}
}
答案 0 :(得分:1)
只需将其添加到您的AddressAndPayment
GET
操作方法:
//GET: /Checkout/AddressAndPayment
public ActionResult AddressAndPayment(int userId)
{
var user = storeDB.UserProfiles.FirstOrDefault(u => u.Id == Id);
var billing = new Billing();
billing.AdresseBilling = user.Adresse;
//etc... add anything else you need here
return View(billing);
}
由于您似乎要求用户登录才能实现这一目标,因此您可以从当前登录的用户处获取id
。