2013-07-11 3 views
5

में ग्रिड.एमवीसी का उपयोग करके रिकॉर्ड संपादित करने के लिए पॉपअप संवाद मैं http://gridmvc.azurewebsites.net/ पर उपलब्ध ग्रिड.एमवीसी का उपयोग कर रहा हूं, जो अच्छी तरह से ग्रिड में डेटा प्रदर्शित करने के लिए कार्यक्षमता प्रदान करता है, जहां फ़िल्टरिंग, सॉर्टिंग, पेजिंग अच्छी तरह से किया जाता है। इस तरह ग्रिड में डेटा इस समय दिखता है।एएसपी.नेट एमवीसी 3

GridData Display

अब तक तो अच्छा। और डेटा प्रदर्शित करने के लिए मैं निम्नलिखित नियंत्रक का उपयोग कर रहा .cshtml

नियंत्रक

/// <summary> 
    /// Brings List Of Customers 
    /// </summary> 
    /// <returns></returns> 
    [HttpGet] 
    public ActionResult CustomerList() 
    { 
     CustomerListViewModel custList = new CustomerListViewModel(); 
     List<CustomerViewModel> custVMList = new List<CustomerViewModel>(); 
     custVMList = custRepository.GetCustomers(); 
     custList.customers = custVMList; 
     return View(custList); 
    } 

.cshtml उसी के लिए है

@model IEnumerable<DataAccess.Models.CustomerViewModel> 
@*Using Twitter Bootstrap API*@ 
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" type="text/css" /> 
<script src="@Url.Content("~/Scripts/gridmvc.min.js")" type="text/javascript"> </script> 
<script src="@Url.Content("~/Scripts/js/bootstrap.min.js")" type="text/javascript"> </script> 
<link href="@Url.Content("~/Content/bootstrap/css/bootstrap.min.css")" rel="stylesheet" type="text/css" /> 
<link href="@Url.Content("~/Content/bootstrap/css/bootstrap-responsive.min.css")" rel="stylesheet" type="text/css" /> 

@using GridMvc.Html 
@{ 
    ViewBag.Title = "Customers List"; 
} 
@Html.Grid(Model).Columns(columns => 
         { 

          columns.Add(m => m.CustomerName).Titled(" Name ").Sortable(true).SortInitialDirection(GridMvc.Sorting.GridSortDirection.Ascending).Filterable(true); 
          columns.Add(m => m.Address1).Titled("Address1").Sortable(true).Filterable(true); 
          columns.Add(m => m.Address2).Titled("Address2").Sortable(true).Filterable(true); 
          columns.Add(m => m.City).Titled("City").Sortable(true).Filterable(true); 
          columns.Add(m => m.County).Titled("County").Sortable(true).Filterable(true); 
          columns.Add(m => m.ContactName).Titled("Contact Name").Sortable(true).Filters.ToString(); 
          columns.Add(m => m.Email).Titled("Email Address").Sortable(true).Filterable(true); 
          columns.Add(m => m.ReferenceNumber).Titled("Reference Number").Sortable(true).Filterable(true); 
          columns.Add(m => m.ModifiedOn).Titled("Modified On").Filterable(true).Sortable(true); 
          columns.Add(m => m.CustomerId) 
           .Titled("Edit") 
           .Sanitized(false) 
           .Encoded(false) 
           //.RenderValueAs(o => Html.ActionLink("Edit", "EditCustomer", "Customer", new { CustomerId = o.CustomerId }, new { title = "Please click here to edit the record", @class = "modal-link" }).ToHtmlString()); 
          .RenderValueAs(d => Html.ActionLink("Edit", "EditCustomer", "Customer", new { CustomerId = d.CustomerId }, new { @class = "modal-link" })); 

         }).WithPaging(4) 
<br /> 
<br /> 
@Html.ActionLink("Click to Add Customer", "AddCustomer", "Customer", new { @class = "modal-link" }) 
<!-- Modal --> 
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 
    aria-hidden="true"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> 
      ×</button> 
     <h3 id="myModalLabel"> 
      Edit Customer</h3> 
    </div> 
    <div class="modal-body"> 
     <p> 
      Loading…</p> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 
      Close</button> 
    </div> 
</div> 
<script type="text/javascript"> 
    //this script reset modal each time when you click on the link: 
    $(function() { 
     $(".modal-link").click(function (event) { 
      event.preventDefault(); 
      $('#myModal').removeData("modal"); 
      $('#myModal').modal({ remote: $(this).attr("href") }); 
     }); 
    }) 
</script> 

जब मैं संपादित करें बटन पर क्लिक करें, नीचे की तरह पॉपअप विंडो में पूरा रिकॉर्ड लोड। वैसे यह ट्विटर बूटस्ट्रैप शैलियों का उपयोग कर रहा है।

Popup dialog box showing data for edit

अब तक तो अच्छा।

नियंत्रक और .cshtml हैं

/// <summary> 
    /// Brings a Specific Customer 
    /// </summary> 
    /// <param name="CustomerId"></param> 
    /// <returns></returns> 
    [HttpGet] 
    public ActionResult EditCustomer(Guid CustomerId) 
    { 
     CustomerViewModel cusVM = custRepository.GetCustomer(CustomerId); 
     return View(cusVM); 

    } 

    /// <summary> 
    /// Editing Customer 
    /// </summary> 
    /// <param name="cusVM"></param> 
    /// <returns></returns> 
    [HttpPost] 
    public ActionResult EditCustomer(CustomerViewModel cusVM) 
    { 
     if (ModelState.IsValid) 
     { 
      custRepository.EditCustomer(cusVM); 
      return RedirectToAction("CustomerList", "Customer"); 
     } 
     else 
     { 
      return PartialView(cusVM); 
     } 
    } 

ग्राहक संपादित करें के लिए .cshtml

@model DataAccess.Models.CustomerViewModel 
@{ 
    Layout = null; 
} 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset>  
     <div class="editor-label"> 
      @Html.LabelFor(model => model.CustomerName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CustomerName) 
      @Html.ValidationMessageFor(model => model.CustomerName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Address1) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Address1) 
      @Html.ValidationMessageFor(model => model.Address1) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Address2) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Address2) 
      @Html.ValidationMessageFor(model => model.Address2) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.City) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.City) 
      @Html.ValidationMessageFor(model => model.City) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.County) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.County) 
      @Html.ValidationMessageFor(model => model.County) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.ContactName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ContactName) 
      @Html.ValidationMessageFor(model => model.ContactName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Email) 
      @Html.ValidationMessageFor(model => model.Email) 
     </div> 
     <div> 
      @Html.HiddenFor(model => model.CustomerId) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.ReferenceNumber) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ReferenceNumber) 
      @Html.ValidationMessageFor(model => model.ReferenceNumber) 
     </div> 
     <p> 
      <input type="submit" value="Save" class="btn btn-primary" /> 
     </p> 
    </fieldset> 
} 

मैं सर्वर साइड सत्यापन का उपयोग कर रहा है। ग्राहक मॉडल है।

using System.ComponentModel.DataAnnotations; 
using System; 
namespace DataAccess.Models 
{ 
    /// <summary> 
    /// Class Holds the List Of Properties of a Customer 
    /// </summary> 
    public class CustomerViewModel 
    { 
     [Required] 
     [DataType(DataType.Text)] 
     [Display(Name = "Customer Name")] 
     public string CustomerName { get; set; } 

      [Required] 
     [DataType(DataType.Text)] 
     [Display(Name = "Address1")] 
     public string Address1 { get; set; } 

      [Required] 
      [DataType(DataType.Text)] 
      [Display(Name = "Address2")] 
      public string Address2 { get; set; } 

      [Required] 
      [DataType(DataType.Text)] 
      [Display(Name = "City")] 
      public string City { get; set; } 


      [Required] 
      [DataType(DataType.Text)] 
      [Display(Name = "County")] 
      public string County { get; set; } 

      [Required] 
     [DataType(DataType.Text)] 
     [Display(Name = "ContactName")] 
     public string ContactName { get; set; } 

      [Required] 
     [DataType(DataType.Date)] 
     [Display(Name = "Email")] 
     public string Email { get; set; } 

     [DataType(DataType.Text)] 
     public Guid CustomerId { get; set; } 


     [DataType(DataType.Text)] 
     public string ReferenceNumber { get; set; } 

     [DataType(DataType.Date)] 
     public DateTime ModifiedOn{ get; set; } 

    } 
} 

जब कोई सत्यापन त्रुटियां नहीं होती हैं तो यह डेटा को सहेजती है और ग्राहक सूची ग्रिड पृष्ठ लोड करती है।

समस्या

जब सत्यापन त्रुटियों सत्यापन संदेश के साथ एक EditCustomer करने के लिए अपने पुन: निर्देशित कर रहे हैं। पॉपअप विंडो में सत्यापन त्रुटियों को प्रदर्शित करने के लिए मैं कैसे कर सकता हूं।

यह एक सादे पृष्ठ में त्रुटियों को प्रदर्शित करने का तरीका है।

Errors should be displayed in a Popup Window, but reloading page in different URL.

मैं पॉपअप विंडो में त्रुटियों को प्रदर्शित करने के लिए कैसे कर सकता हूं।

उत्तर

3

आपको AJAX सत्यापन और क्लाइंट साइड सत्यापन पर अधिक बारीकी से देखने की आवश्यकता है। असल में जो हो रहा है वह आंशिक दृश्य है जिसे आप लोड कर रहे हैं जिसमें आपके संपादन फ़ॉर्म में प्रारंभिक पृष्ठ लोड के बाद लोड होने के बाद से सत्यापन मान्य नहीं है। आप इसे अपने पृष्ठ (JQuery) में जोड़ने का प्रयास कर सकते हैं:

$.validator.unobtrusive.parse('#formId'); 

जहां फॉर्म आपके HTML फॉर्म की आईडी है। आप एचटीएमएल हेल्पर के बजाय अजाक्स.बिनफॉर्म हेल्पर का उपयोग करने की भी आवश्यकता है।

इसके अलावा पोस्ट पर एक नज़र डालें:

ASP.NET MVC client validation with partial views and Ajax

+0

हाँ मार्को। धन्यवाद। मैंने JQuery Validate को अविभाज्य और मान्य स्क्रिप्ट को रखा है। अब पृष्ठ पॉपअप संवाद में सभी त्रुटियों के साथ लोड होता है। धन्यवाद। –

संबंधित मुद्दे