Asp.net MVC c#一个表单,两个模型
本文关键字:两个 模型 表单 一个 MVC Asp net | 更新日期: 2023-09-27 17:49:20
我有如下两个模型:
public class Bill
{
public int Id { get; set; }
public string InvoiceNumber { get; set; }
public Int64 Amount { get; set; }
public int? NewPaymentId { get; set; }
public virtual NewPayment RelPayment { get; set; }
}
public class NewPayment
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LstName { get; set; }
public DateTime PaymentDate { get; set; }
public Int64 ProvisionNumber { get; set; }
public Int64 CreditCardNumber { get; set; }
public int ExpMonth { get; set; }
public int ExpYear { get; set; }
public int Cv2 { get; set; }
public Int64 Amount { get; set; }
public string UserId { get; set; }
public string CustomerNote { get; set; }
}
客户将在我的应用程序中通过信用卡支付他的发票。
我有一个视图,我张贴NewPayment模型的动作。但现在,我还需要发送哪些发票将支付。所以我需要为比尔模型创建一个表单,我想?但是我不知道我怎么能把两个模型传递到同一个动作,我不知道NewPaymentId之前执行支付方法。
关于注释:
我的组合模型如下:
public class Payment
{
public IEnumerable<Bill> Bill { get; set; }
public NewPayment NewPayment { get; set; }
}
我的观点如下:
@model IEnumerable<ModulericaV1.Models.Bill>
<form class="form-no-horizontal-spacing" id="NewPayment" action="/NewPayment/AddInvoice" method="post">
<div class="row column-seperation">
<div class="col-md-6">
<h4>Kart Bilgileri</h4>
<div class="row form-row">
<div class="col-md-5">
<input name="FirstName" id="FirstName" type="text" class="form-control" placeholder="Kart Üzerindeki Ad">
</div>
<div class="col-md-7">
<input name="LastName" id="LastName" type="text" class="form-control" placeholder="Kart Üzerindeki Soyad">
</div>
</div>
<div class="row form-row">
<div class="col-md-12">
<input name="CreditCardNumber" id="CreditCardNumber" type="text" class="form-control" placeholder="Kart Numarası">
</div>
</div>
<div class="row form-row">
<div class="col-md-5">
<input name="ExpYear" id="ExpYear" type="text" class="form-control" placeholder="Son Kullanma Yıl (20..)">
</div>
<div class="col-md-7">
<input name="ExpMonth" id="ExpMonth" type="text" class="form-control" placeholder="Son Kullanma Ay (1-12)">
</div>
</div>
<div class="row form-row">
<div class="col-md-5">
<input name="Cv2" id="Cv2" type="text" class="form-control" placeholder="Cv2">
</div>
<div class="col-md-7">
<input name="Amount" id="Amount" type="text" class="form-control" placeholder="Miktar TL ">
</div>
</div>
<div id="container">
<input id="Interests_0__Id" type="hidden" value="" class="iHidden" name="Interests[0].Id"><input type="text" id="InvoiceNumber_0__InvoiceNumber" name="[0].InvoiceNumber"><input type="text" id="Interests_0__InterestText" name="[0].Amount"> <br><input id="Interests_1__Id" type="hidden" value="" class="iHidden" name="Interests[1].Id"><input type="text" id="InvoiceNumber_1__InvoiceNumber" name="[1].InvoiceNumber"><input type="text" id="Interests_1__InterestText" name="[1].Amount"> <br>
</div>
<input type="button" id="btnAdd" value="Add New Item" />
<button class="btn btn-danger btn-cons" type="submit"> Ödemeyi Gerçekleştir</button>
</form>
</div>
</div>
在我的控制器,我得到支付模型为空。
public ActionResult AddInvoice(Payment payment) {
foreach (var item in payment.Bill)
{
var Billing = new Bill();
Billing.Amount = item.Amount;
Billing.InvoiceNumber = item.InvoiceNumber;
db.Bill.Add(Billing);
db.SaveChanges();
}
return View();
}
}
我用一个例子来完成Marko
public class CombineModel
{
public Bill Bill{ get; set; }
public NewPayment NewPayment{ get; set; }
}
您的模型中似乎已经有了解决方案。您的bill对象可以保存对相关新付款的引用。你可以从数据库中延迟读取新的支付,也可以在发送到视图之前为账单分配一个新的newpayment对象。
视图模型是很好的实践,但是您可能会很高兴地利用您所拥有的模型,就像我刚才描述的那样。
对不起,这里应该是:
- 另一种方式-通过NewPayment
- 添加
public IEnumarable<Bill> Bills {get; set;}
到NewPayment模型
这样,您就可以访问与给定付款相关联的Bills。
Code first stuff:
- 你应该用
[ForeignKey("NewPaymentID"]
装饰Bill的RelPayment,这样EF(我假设你正在使用实体框架)知道如何连接关系。 - 您可能还需要将以下
Bills = new List<Bill>();
添加到NewPayment构造函数中。
如果你不喜欢Zakos的解决方案,你可以创建tuple:
var tuple= new Tuple<Bill,NewPayment>(obj1,obj2);
你将看到:
@model Tuple<Bill,NewPayment>
但是你应该使用@Zakos的解决方案
所以你可以使用ViewModel,取这个ViewModel:
public class PaymentBillViewModel
{
public int BillId { get; set; }
public int PaymentId { get; set; }
public string InvoiceNumber { get; set; }
public Int64 Amount { get; set; }
public int? NewPaymentId { get; set; }
public virtual NewPayment RelPayment { get; set; }
public int Id { get; set; }
public string FirstName { get; set; }
public string LstName { get; set; }
public DateTime PaymentDate { get; set; }
public Int64 ProvisionNumber { get; set; }
public Int64 CreditCardNumber { get; set; }
public int ExpMonth { get; set; }
public int ExpYear { get; set; }
public int Cv2 { get; set; }
public Int64 Amount { get; set; }
public string UserId { get; set; }
public string CustomerNote { get; set; }
}
实际上把你需要的东西放在视图中。然后在post动作中将ViewModel转换为相关的Model:
[HttpPost]
public ActionResult Sample(PaymentBillViewModel model)
{
if (ModelState.IsValid)
{
var obj=new NewPayment
{
LstName= model.LstName,
Amount=model.Amount,
//... cast what else you need
}
}
return View();
}
可以使用Automapper进行强制转换,有关使用Automapper的更多信息,请参阅本文。