ドロップダウンリスト、ラジオボタン、テキストボックス、ラジオボタンを単一の形式で使用しています。バリデーターを使用RequiredIf
しました。フォームを送信すると、バリデータは機能しますが、ドロップダウン リストとラジオ ボタンの状態が変わります。送信後に値を変更しないように選択する必要があります。これを行う方法 My Code is.
意見:
@*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>
コントローラ:
[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);
}
モデル:
#region[Public Properties]
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();
#endregion[Public Properties]