我有一个表单,它将绑定两个不同的下拉列表中的一些值,并且我保存用户选择的值。现在我使用RequiredIf attributes.Its也可以正常工作。如果用户错过选择它显示消息的值,同样,在点击提交按钮后,下拉菜单中的一些选定值将默认为默认值。由于操作结果再次载入,我需要显示差异而用户选择没有任何变化。如何在Asp.net MVC4中避免回传
My Code for Model is;
> public ObservableCollection<Receipt> GetReceiptList()
> {
> ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
> DataTable dtReceipt = new DataTable();
> dtReceipt = objDAL.ExecuteTable(CommandType.StoredProcedure, "sp_Receipt_Select");
> foreach (DataRow dr in dtReceipt.Rows)
> {
> ReceiptList.Add(new Receipt
> {
> Id = Convert.ToInt32(dr["REC_Id_I"]),
> Cust_Name = dr["CUS_Name_V"].ToString(),
> Pay_Amount = dr["REC_PaidAmount_M"].ToString(),
> Pay_Mode = dr["REC_PayMode_C"].ToString(),
> Bank_Name = dr["REC_BankName_V"].ToString(),
> Bank_Address = dr["REC_BankAddress"].ToString(),
> ChequeNo = dr["REC_ChequeNo_V"].ToString(),
> Cheque_Date = dr["REC_ChequeDate_D"].ToString(),
> });
> }
> return ReceiptList;
> }
的控制代码
//AtLoad
public ActionResult ReceiptMaster(Receipt model)
{
ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
Receipt Receipt = new Models.Receipt();
ReceiptList = Receipt.GetReceiptList();
ModelState.Clear();
Sales sales = new Models.Sales();
DataTable dtCustomer = new DataTable();
dtCustomer = sales.GetCustomerList();
IList<Sales> MyCustList = new List<Sales>();
foreach (DataRow mydataRow in dtCustomer.Rows)
{
MyCustList.Add(new Sales()
{
Cust_Id = Convert.ToInt32(mydataRow["Id"].ToString().Trim()),
Cust_Name = mydataRow["Name"].ToString().Trim()
});
}
var CustName = new SelectList(MyCustList, "Id", "Cust_Name");
ViewData["Cu_Name"] = CustName;
return View(ReceiptList);
}
//TO Insert
[HttpPost]
public ActionResult ReceiptMaster(Receipt model, string command)
{
Receipt Receipt = new Models.Receipt();
if (command == "Sumbit")
{
int Id = 0;
if (model.Pay_Mode == "C")
{
model.ChequeNo = "";
model.Cheque_Date = ("1/1/1753 12:00:00 AM");
model.Bank_Name = "";
model.Bank_Address = "";
}
if (ModelState.IsValid)
{
Id = Receipt.SaveReceipt(model.Id, model.Cust_Id, model.Pay_Amount, model.Pay_Mode, model.Bank_Name, model.Bank_Address, model.ChequeNo, model.Cheque_Date);
if (Id > 0)
{
ViewData["Success"] = "Product was saved successfully.";
ViewData["ControlView"] = 1;
return RedirectToAction("ReceiptMaster", "Admin");
}
return RedirectToAction("ReceiptMaster", "Admin");
}
ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
ReceiptList = Receipt.GetReceiptList();
return View(ReceiptList);
}
ObservableCollection<Receipt> ReceiptList1 = new ObservableCollection<Receipt>();
ReceiptList1 = Receipt.GetReceiptList();
return View(ReceiptList1);
}
脚本在下拉用于绑定值
<script type="text/javascript">
$(document).ready(function() {
$.post('@Url.Action("SelectCustomerForDropJson", "Admin")', null, function (data) {
var select = $("#Cust_Id");
select.empty();
select.append($('<option/>', { value: '', text: '--Select--' }));
$.each(data, function (index, Data) {
select.append($('<option/>', {
value: Data.Value,
text: Data.Text
}));
});
});
});
</script>
先生其罚款......但我用同样的形式的网格,所以它会显示绑定错误。 –
现在更改我的代码,请参阅并尝试 –
对不起,先生,现在也是它的作品我以前的.. –