0

我正在使用Dropdownlist,Radiobutton,TextboxRadio 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(); 
4

1 回答 1

0

我通过在 View 上使用Viewdata()andScript和一些修改来做到这一点......我修改后的代码是

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" />
于 2013-10-07T06:19:55.173 回答