简体   繁体   English

从UserProfile Table和Billing table字段自动填充视图

[英]auto-populate a view from the UserProfile Table and the Billing table fields

I will like to get the AddressAndPayment view being auto-populate from the UserProfile table. 我想从UserProfile表中自动填充AddressAndPayment视图。

I will like to use the fields from the UserProfile table to be auto-displayed in the AddressAndPayment.cshtml. 我想使用UserProfile表中的字段在AddressAndPayment.cshtml中自动显示。 I can't figure out how to perform this. 我不知道如何执行此操作。

Some fields needs to come from Billing.cs and some other from the UserProfile Table. 有些字段需要来自Billing.cs,而另一些字段则需要来自UserProfile表。

Thanks. 谢谢。

The view I wanted to be populate is 我想填充的视图是

AddressAndPayment.cshtml 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>

Here are the field I store in the UserProfileTable from the AccountModels.cs 这是我从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 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 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);

         }

     }
 }

Just add this to your AddressAndPayment GET action method: 只需将其添加到您的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);
}

Since it seems you are requiring that the user be logged in to get this far, then you could get the id from the current logged in user. 由于似乎您需要登录用户才能到达此距离,所以您可以从当前登录的用户那里获取id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM