Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Tuesday, November 27, 2012

Read Telerik drop downlist javascript

sometimes we need to read the value/text of a Telerik MVC dropDownList  items.
using JQUERY:

function getData()
{
var items= $('#dropdownlistName).data('tDropDownList').data;
// items is the items list
// items can be dealt as an array
// then we can get the text and the value of each item as following:
 
var text = items[0].Text;
var value = items[0].Value;
 
 
}

Note :
you have to read $('#dropdownlistName) " after document.read() function as we have to wait for  the Telerik JQuery library to be loaded.
it is preferred to be read pageLoad function of the Microsoft.Ajax library

function pageLoad()
{
    getData()
}

or at any event handler javascript function

Thursday, December 22, 2011

RDLC reports at MVC project


RDLC reports cannot have connection string and can't directly connected to the database , instead we use the "Object" datasource to provide data to the reports.

The "Object" datasource provides, at the design time , the report by the structure of the data that  will be passed to the report at the runtime

The datasources cannot be added to the MVC project so we need a class library that the datasources will be built upon

Steps

  1. create a class library project 
  2. add the "ViewModel" class -- that will have the structure of the data 
  3. for example
public class PatientViewModel
{
public int Id{get;set;}
public string Name{get;set;}
}
    4. at the solution explorer , select the ViewModelClassLibrary project
    5. at the menu bar => Data => select =>Add New DataSource 
    6. Data Source configuration wizard open => select "Object" Data source =>Next
    7.  select "PatientViewModel " class => finish
    8.create the MVC project 
   9. add new folder called "forms" at the project main folder
  10. add new rdlc report to the folder 
  11 . open the report => at the view menu => press " report data"
12 .create new datasets  => in the list , you will find the created data source at point  (7)
 and give the dataset a specific name => for example => PatientDataSet
13 . at the controller  , create an action for the report
 public ActionResult GetReport()
        {
            LocalReport rep = new LocalReport();
            rep.ReportPath = Server.MapPath("~/Forms/MyReport.rdlc");
            List<PatientViewModel> patients = GetPatients();
            rep.DataSources.Add(new ReportDataSource("PatientDataSet", patients));
 
            //Render the report
            _renderedBytes = rep.Render(
                _reportType,
                _deviceInfo,
                out _mimeType,
                out _encoding,
                out _fileNameExtension,
                out _streams,
                out _warnings);
 
            return File(_renderedBytes, _mimeType);
 
        }





Multiple validation summary at single page

The problem:
at razor , if we have 3 partial views each contains a validation summary control
when the validation works, and the validation summary supposed to give the usual red text , all the validations summary controls will view the error , even if the element that have the problem is not it its own view
example 

The solution:
the solution is to draw the validation summary only when you validate your form for example
  1. at the controller of "RegisterForm" , set the  Session["ValidationSummary"] = "RegisterForm"
  2. at the controller of "LoginForm" , set the  Session["ValidationSummary"] =  "LoginForm"
  3. at the   RegisterForm view
                    if ( Session["ValidationSummary"] == "RegisterForm")
            { 
   @Html.ValidationSummary(); 
            } 

    4. at the    LoginForm  view 
       if ( Session["ValidationSummary"] == " LoginForm ")
            { 
                   @Html.ValidationSummary(); 
            } 

       



you can find nother solution here NamedValidationSummary 



Error with MVC telerik


when using Telerik components , it may give you the erorr:
 "Unable to set value of the property '__MVC_FormValidation': object is null or undefined"
 solution :
 let the style sheets registeration to be:
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css") ) )

 instead of :
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css")
                                                                                  .Combined(true)
                                                                                   .Compress(true) ) )