在mvc 4 ASP.net中提交表单后保存下拉列表状态和单选按钮状态

时间:2013-09-30 05:48:00

标签: c# asp.net asp.net-mvc

我以单一形式使用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>

控制器:

[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]

1 个答案:

答案 0 :(得分:0)

提交后,您可以使用ViewData传递数据y并进入“查看”以显示所选值:

单选按钮:

<强>控制器

ViewData["Pay_mode] = Pay_mode;

查看

<script type="text/javascript">

    $(document).ready(function () { var pay_mode = '@ViewData["Pay_mode"]';
        if (pay_mode == "C") {

            document.getElementById("Cash").checked = true;
            chMd();
        } else {

            document.getElementById("Cheque").checked = true;
            chMd();

        }
       });
</script>

下拉列表:

<强>控制器

 ViewData["Cu_Name"] = CustName;

查看

<script type="text/javascript" language="javascript">
    $(function () {
        $('#Cust_Id').change(function () {
            var val = ($(this).find(":selected").text());
            document.getElementById("Cust_Name").value = val;

        });

    });

</script>