Asp.net MVC C# 一种形式,两种模式
Posted
技术标签:
【中文标题】Asp.net MVC C# 一种形式,两种模式【英文标题】:Asp.net MVC C# One Form, Two Model 【发布时间】:2014-04-12 12:33:50 【问题描述】:我有两个模型,如下所示。
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 模型发布到操作中。但是现在,我还需要发送哪些发票将被支付。所以我需要为我认为的 Bill 模型再创建一个表格?但是我不知道如何将两个模型传递给相同的操作,并且在执行付款方式之前我不知道 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();
【问题讨论】:
你不能通过两个模型。但是您可以做的是将您的两个模型组合成一个模型并将该模型传递给您的操作,然后决定在您的业务逻辑或控制器中如何处理它。 其实我也不懂,多一张发票,一票全款。 见下面 Zakos 的回答... 【参考方案1】:我用一个例子来完成 Marko
public class CombineModel
public Bill Bill get; set;
public NewPayment NewPayment get; set;
【讨论】:
如果我想传递多个 Bill 对象怎么办?我需要更改为 IEnumerable 吗? IEnumerable , List , w/e 让你开心 好的,我已经用 public IEnumerable您的模型中似乎已经有了解决方案。您的 bill 对象可以包含对相关新付款的引用。您可以懒惰地从数据库中读取新的付款,也可以在发送到视图之前为账单分配一个新的 newpayment 对象。
查看模型是一种很好的做法,但您可能会很乐意利用我刚才描述的自然模型。
更新
对不起,这应该是:
-
反过来 - 传入 NewPayment
将
public IEnumarable<Bill> Bills get; set;
添加到NewPayment 模型
这样,您就可以访问与给定付款相关的账单。
代码优先:
您应该使用[ForeignKey("NewPaymentID"]
装饰Bill 的RelPayment,因此EF(我假设您使用实体框架)知道如何连接关系。
您可能还需要将以下 Bills = new List<Bill>();
添加到 NewPayment 构造函数中。
【讨论】:
但每次付款会有不止一张发票。 @ukmi - 我认为我的回答仍然有效。我已经更新了它以解决我的误解。 我明白了,我误解了你的问题,我没有意识到你是从客户端发布到控制器的,我认为这是相反的方式 - 愚蠢的我。我会删除这个答案。【参考方案3】:如果您不喜欢 Zakos 解决方案,您可以制作元组:
var tuple= new Tuple<Bill,NewPayment>(obj1,obj2);
在视图中你将拥有:
@model Tuple<Bill,NewPayment>
但你应该使用@Zakos 解决方案。
【讨论】:
【参考方案4】:所以你可以使用 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 转换为相关的模型:
[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 的更多信息,请查看this 文章。
【讨论】:
以上是关于Asp.net MVC C# 一种形式,两种模式的主要内容,如果未能解决你的问题,请参考以下文章