我以单一形式使用Dropdownlist,Radiobutton,Textbox
和Radio Buttons
。我使用RequiredIF
验证器。当我提交时,验证器可以正常工作,但dropdowmlist和单选按钮状态将会改变。我需要选择提交后不能更改的值。如何执行此操作?
我的代码是。
查看:
@*DropDownlist*@
<select id="Cust_Id" name="Cust_Id" class="dropdownlist"> </select>
@*RadioButton*@
<input name="Pay_Mode1" value="false" onclick="chMd()" type="radio"
checked="checked" />Cash <input name="Pay_Mode1" onclick="chMd()"
type="radio" value="true" />Cheque
@*Grid*@
<div id="divgrid" style="margin-top: 15px;">
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow",
ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);}
<div id="gridContent">
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
grid.Column("Id", "ID", style: "id"),
grid.Column("Cust_Name", "Cust Name", style: "PName"),
grid.Column("Pay_Amount", "Pay_Amount", style: "ICode"),
grid.Column("Pay_Mode", "Pay_Mode", style: "IName"),
grid.Column("Bank_Name", "Bank_Name", style: "Weight"),
grid.Column("Bank_Address", " Bank_Address", style: "MakingCharge"),
grid.Column("ChequeNo", "ChequeNo", style: "Certification"),
grid.Column("Cheque_Date", " Cheque_Date", style: "Price"),
grid.Column(header: "Delete", format: @<text><a href="@Url.Action("DeleteReceipt", "Admin", new { Id
= item.ID })" onclick="javascript:return confirm('Are you sure you'd like to delete this product?');"><img
src="../Images/delete.png" alt='Delete' /></a></text>)
))
@if (grid.HasSelection)
{
Receipt = (JewellaryWeb.Models.Receipt)grid.Rows[grid.SelectedIndex].Value;
<b>Id</b> @Receipt.Id<br />
<b>Code</b> @Receipt.Cust_Name<br />
<b>Item_Code</b> @Receipt.Pay_Amount<br />
<b>Item_Name</b> @Receipt.Pay_Mode<br />
<b>Weight</b> @Receipt.Bank_Name<br />
<b>Making_Charge</b> @Receipt.Bank_Address<br />
<b>Certification</b> @Receipt.ChequeNo<br />
<b>Price</b> @Receipt.Cheque_Date<br />
}
</div>
Controller:
[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;
//ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
model.ReceiptList = Receipt.GetReceiptList();// model.ReceiptList is your model property
return View(model.ReceiptList);
}
// ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
model.ReceiptList = Receipt.GetReceiptList();// model.ReceiptList is your model property
return View(model.ReceiptList);
}
model.ReceiptList = Receipt.GetReceiptList();// model.ReceiptList is your model property
return View(model.ReceiptList);
//ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
//ReceiptList = Receipt.GetReceiptList();
//return View(ReceiptList);
}
//ObservableCollection<Receipt> ReceiptList = new ObservableCollection<Receipt>();
model.ReceiptList = Receipt.GetReceiptList();// model.ReceiptList is your model property
return View(model.ReceiptList);
}
型号:
public ObservableCollection<Receipt> ReceiptList { get; set; }
DataAccessLayer objDAL = new DataAccessLayer();
[DisplayFormat(ConvertEmptyStringToNull = false)]
public Int32 Id { get; set; }
[Required(ErrorMessage = "Please Select the Name")]
public Int32 Cust_Id { get; set; }
public string Name { get; set; }
public string Cust_Name { get; set; }
public string Date { get; set; }
[Required(ErrorMessage = "*")]
[StringLength(10, ErrorMessage = "Maximum {1} characters exceeded")]
[RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "{0} must be a Number.")]
public string Pay_Amount { get; set; }
[StringLength(10, ErrorMessage = "Maximum {1} characters exceeded")]
public string Credit { get; set; }
[StringLength(10, ErrorMessage = "Maximum {1} characters exceeded")]
public string Pay_Mode { get; set; }
public bool Pay_Mode1 { get; set; }
public bool IsChequeRequired
{
get
{
return Pay_Mode1;
}
}
[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must enter Bank Name !")]
[StringLength(10, ErrorMessage = "Maximum {1} characters exceeded")]
public string Bank_Name { get; set; }
[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must enter Bank Address !")]
//[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must explain any \"Yes\" answers!")]
[StringLength(20, ErrorMessage = "Maximum {1} characters exceeded")]
public string Bank_Address { get; set; }
//[Required(ErrorMessage = "*")]
[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must enter ChequeNo !")]
[StringLength(20, ErrorMessage = "Maximum {1} characters exceeded")]
public string ChequeNo { get; set; }
// [Required(ErrorMessage = "*")]
[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must enter Cheque Date !")]
[StringLength(10, ErrorMessage = "Maximum {1} characters exceeded")]
public string Cheque_Date { get; set; }
//[RequiredIf("IsChequeRequired", true, ErrorMessage = "You must enter Cheque Date !")]
//[StringLength(20, ErrorMessage = "Maximum {1} characters exceeded")]
public string Credited { get; set; }
public SelectList Ddl { get; set; }
public List<SelectListItem> SelectedValue { get; set; }
DataTable dt = new DataTable();
答案 0 :(得分:0)
我使用Viewdata()
和Script
完成了此操作,并对View进行了一些修改......我的修改后的代码是
Controller:
ViewData["PAYMODE"] = model.Pay_Mode;
ViewData["Credit"] = model.Credited;
ViewData["CustName"] = model.Cust_Name;
In View:
Script
<script type="text/javascript">
$(document).ready(function () {
var val = ('@ViewData["PAYMODE"]');
var Credit = ('@ViewData["Credit"]');
if (val == "C") {
document.getElementById("Cash").checked = true;
chMd();
} else {
document.getElementById("Cheque").checked = true;
chMd();
}
if (Credit == "N") {
document.getElementById("CreditNo").checked = true;
chMd1();
}
else {
document.getElementById("CreditYes").checked = true;
chMd1();
}
});
</script>
Design:
<input name="dOption1" id="CreditYes" value="Y" onclick="chMd1()" type="radio" />Yes
<input name="dOption1" id="CreditNo" value="N" onclick="chMd1()" type="radio" checked="checked"/>No
<input name="Credited" type="text" style="visibility: hidden; width: 1px;" value="n" />