我有一个部分视图,返回用户地址列表,我需要用户能够选择一个,然后当他们按下"提交"连同其他细节,它从所选地址的细节返回到控制器,从那里我知道如何分配它我想要的地方。我该怎么做?示例和帮助表示赞赏
这是我的控制器,用于创建订单,在下方查看;
[HttpPost]
public ActionResult AddressAndPayment(FormCollection values, string id)
{
var order = new Order();
TryUpdateModel(order);
//sets date and username
order.Username = User.Identity.Name;
order.OrderDate = DateTime.Now;
//Order gets saved
storeDB.Orders.Add(order);
storeDB.SaveChanges();
//Order gets processed
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.CreateOrder(order);
//Save again, total not saving properly until now
storeDB.SaveChanges();
return RedirectToAction("Complete",
new { id = order.OrderId });
}
查看
@model T_shirt_Company_v3.Models.Order
@{
ViewBag.Title = "Address And Payment";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(x => x.OrderId)
@Html.HiddenFor(x => x.OrderDate)
@Html.HiddenFor(x => x.PaymentTransactionId)
@Html.HiddenFor(x => x.Total)
@Html.HiddenFor(x => x.Username)
<div class="form-horizontal">
<h2>Address And Payment</h2>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@{Html.RenderAction("Listofaddresses", "Checkout");}
<h2>Select Postage type</h2>
@Html.EnumDropDownListFor(model => model.PostageList, "-- Please select postage type --")
<h2>Payment</h2>
<div class="form-group">
@Html.LabelFor(model => model.cardDetails.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cardDetails.FullName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cardDetails.FullName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cardDetails.CardNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cardDetails.CardNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cardDetails.CardNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cardDetails.CardExpireMonth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="row">
<div class="col-md-1">
@Html.EditorFor(model => model.cardDetails.CardExpireMonth, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cardDetails.CardExpireMonth, "", new { @class = "text-danger" })
</div>
<div class="col-md-1">
@Html.EditorFor(model => model.cardDetails.CardExpireYear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cardDetails.CardExpireYear, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cardDetails.Cvc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cardDetails.Cvc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cardDetails.Cvc, "", 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>
}
部分视图
@model IEnumerable<T_shirt_Company_v3.Models.deliveryAddress>
@foreach (var item in Model)
{
<div style="width: 180px">
<div class="thumbnail">
<p>@Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) </p>
<p>@Html.DisplayFor(modelItem => item.Address)</p>
<p>@Html.DisplayFor(modelItem => item.City)</p>
<p>@Html.DisplayFor(modelItem => item.PostalCode)</p>
<p>@Html.DisplayFor(modelItem => item.Country)</p>
</div>
</div>
}
地址类
public class deliveryAddress
{
[Key]
[ScaffoldColumn(false)]
public int AdressId { get; set; }
[ScaffoldColumn(false)]
public string UsersAddress { get; set; }
[Required]
[StringLength(16, ErrorMessage = "Your name is too long")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Your last name is required.")]
[StringLength(16, ErrorMessage = "Last name is too long.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "Address is required.")]
public string Address { get; set; }
[Required(ErrorMessage = "City is required.")]
public string City { get; set; }
[Required(ErrorMessage = "Postcode is required.")]
[Display(Name = "Post Code")]
public string PostalCode { get; set; }
[Required(ErrorMessage = "Country is required.")]
public string Country { get; set; }
[Required(ErrorMessage = "Phone home number is required.")]
[Display(Name = "Home Telephone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Phone mobile number is required.")]
[Display(Name = "Mobile")]
public string Mobile { get; set; }
}
}