Server-side Validation using MVC 3 - validation

I'm building an ASP.NET MVC3 app. I have 2 views;
List item contains a grid
details view consists of a drop down list (combobox)
I have a requirement to alert the user at the details view when they try to select item was previously selected in the list view. In other words, the grid should contain unique items
What is the best way to implement a server-side business logic validation?
Model:
public class AllocatedResource
{
public virtual Project Project { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
}
List View:
#(Html.Telerik().Grid(Model.AllocatedResources)
.Name("gridAllocatedProject")
.DataKeys(keys =>{keys.Add(p => p.Id);})
.Columns(columns =>
{
columns.Bound(p => p.Id).Visible(false);
columns.Bound(p => p.Project.Name);
columns.Bound(p => p.Project.ProjectManager).Title("Project Manager");
columns.Bound(p => p.StartDate).Width(80).Format("{0:d}");
columns.Bound(p => p.EndDate).Width(80).Format("{0:d}");
})
Details View:
#Html.Label("Project: ")
#(Html.Telerik().ComboBox().Name("Project")
.BindTo(new SelectList(Model.AllProjects, "Id", "Name"))
.Value(Model.AllocatedResource.Project.Id.ToString()))
#Html.Label("Start Date: ")
#(Html.Telerik().DatePicker().Name("StartDate")
.Value(Model.AllocatedResource.StartDate))
#Html.Label("End Date: ")
#(Html.Telerik().DatePicker().Name("EndDate")
.Value(Model.AllocatedResource.EndDate))

Related

Kendo Pie Chart MVC Data Binding

I'm having an issue with Telerik's Kendo UI Pie Chart in MVC. I can't find any decent code examples on their site. The one demo I've found only shows half of the code, so I tried guessing at how to get it working. I'm getting an error, and no matter what I've tried to eliminate the error, nothing seems to work. It's having an issue with the series.Pie section where you actually bind the data. I copied the code that was in their example, and used the lambda expression just like they did. model => model.Percentage and model => model.StatusName to work with my view model. But it errors out on the very first lambda, with this error message:
CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type
Here is the relevant code:
ViewModel / View:
public class StatusPercentageViewModel
{
public string StatusName { get; set; }
public int Count { get; set; }
public double Percentage { get; set; }
}
#model IEnumerable<StatusPercentageViewModel>
#(Html.Kendo().Chart(Model)
.Name("StatusBreakdown")
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.Series(series =>
{
series.Pie(
model => model.Percentage,
model => model.StatusName,
null,
null
);
})
.Tooltip(tooltip => tooltip.
Template("${ category } - ${ value }%").Visible(true)
)
)
You probably need to specify the model if you want to define your series like that. See this example
For example:
#(Html.Kendo().Chart<StatusPercentageViewModel>()
.Name("StatusBreakdown")
.Legend(legend => legend.Position(ChartLegendPosition.Bottom))
.DataSource(ds => ds.Read(read => read.Action("GetStatus", "Status")))
.Series(series =>
{
series.Pie(
model => model.Percentage,
model => model.StatusName
);
})
.Tooltip(tooltip => tooltip.Template("${ category } - ${ value }%").Visible(true))
)
Another way (more similar to what you have now) to do it would be:
#model IEnumerable<StatusPercentageViewModel>
#(Html.Kendo().Chart()
.Name("StatusBreakdown")
.Legend(legend => legend.Position(ChartLegendPosition.Bottom))
.Series(series => series.Pie(Model))
.Tooltip(tooltip => tooltip.Template("${ category } - ${ value }%").Visible(true))
)

how to pass default value to textbox dynamically?

mvc3 razor- i have a bellow model in my web app:
public class mymodel{
[Display(Name = "name")]
public string Name { get; set; }
}
when we use
#Html.LabelFor(m => m.Name)
it use display "name" ,for example upper of our text box to show user this filed is for entering your name.
how can i use display name in text box value?
i test it but value is empty i want to get value from my model from [Display(Name = "name")]
#Html.TextBoxFor(m => m.Name, new {#Value = #Html.ValueFor(m => m.Name)})
#Html.TextBoxFor(m => m.Name, new {Value = Html.DisplayNameFor(m => m.Name)})
EDIT: If you need to re-display the form and preserve the value that the user entered, then you might want to either use Darin's approach, or do something like this:
#Html.TextBoxFor(m => m.Name, new { Value = (string.IsNullOrWhiteSpace(Model.Name) ? Html.DisplayNameFor(x => x.Name).ToHtmlString() : Model.Name) })
In the controller action rendering this view you could set the Name property of your view model:
public ActionResult Index()
{
var model = new MyViewModel();
model.Name = ModelMetadata.FromLambdaExpression<MyViewModel, string>(x => x.Name, new ViewDataDictionary<MyViewModel>(model)).DisplayName;
return View(model);
}
and in your view:
#Html.LabelFor(x => x.Name)
#Html.EditorFor(x => x.Name)

How to create Ajax-support Paging Nested Grid in Telerik MVC?

I try to create a Ajax-support Paging Nested Grid in Telerik MVC Extension. I have rendered the Nested Grid in browse, can expand row Genre to show the related Albums filtered by GenreId, but when i use Ajax for this nested Grid, it don't work in grid paging. I have upload my project and post some code about my View and Controller.
When i don't use ajax, the gird can work properly but it cannot expand correctly when browse is reloaded, how can i maintain the state of row which had expanded?
I'm gratiful for any help.
My Project:
http://share.vnn.vn/dl.php/11718146
My View:
#{
ViewBag.Title = "Home Page";
Layout = #"~\Views\Shared\_Layout.cshtml";
}
#( Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Genre>(#Model)
.Name("personGrid")
.DataKeys(keys => keys.Add(k => k.GenreId))
.DataBinding(d => d.Ajax().Update( "_Update", "Home" )
)
// .Editable(editing => editing.Mode(GridEditMode.PopUp))
.Columns(c =>
{
c.Bound(m => m.Name).Width(150);
c.Bound(m => m.Description).Width(150);
//c.Bound(m => m.Birthdate);
c.Command(commands =>
{
commands.Edit();
}).Width(80);
})
.DetailView(detailView =>
detailView.Template(
#<text>
#(Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Album>(item.Albums)
.Name("Albums_" + item.GenreId)
.DataBinding(d => d.Ajax())
.Columns(columns =>
{
columns.Bound(o => o.Title).Width(101);
columns.Bound(o => o.Price).Width(140);
})
.Pageable()
.Sortable()
.Filterable()
)
</text>
)
.ClientTemplate(
Html.Telerik().Grid<MvcMusicCodeFirsr.Models.Album>()
.Name("Albums_<#=GenreId#>")
.DataBinding(d => d.Ajax())
.Footer(false)
.ClientEvents(events => events.OnDataBinding("detailGrid_dataBinding"))
.ToHtmlString()
))
.Sortable()
.Pageable()
)
)
<script type="text/javascript">
function detailGrid_dataBinding(e) {
var grid = $(this).data("tGrid"),
masterRow = $(this).closest("tr.t-detail-row").prev(),
dataItem = $("#Grid").data("tGrid").dataItem(masterRow);
grid.dataBind(dataItem.Albums);
e.preventDefault();
}
</script>
My Controller:
MusicStoreEntities db = new MusicStoreEntities();
public ActionResult Index()
{
return View(db.Genres.ToList());
}
[GridAction]
public ActionResult _Select()
{
return View(new GridModel ( db.Genres.ToList()));
}
[GridAction]
public ActionResult _Update()
{
return View(new GridModel { Data = db.Genres.ToList() });
}
Here is an example showing ajax pading for the nested grid: http://demos.telerik.com/aspnet-mvc/grid/hierarchyajax

Telerik MVC - Numeric textbox in grid editor

In my editor for insert telerik show only common textbox (not integer,decimal etc).
this is my view:
#(Html.Telerik().Grid <GamePlayer2>(Model).Name("za11").DataKeys(keys => keys.Add(c => c.Id))
.ToolBar(commands => commands.Insert()
.ButtonType(GridButtonType.ImageAndText))
.DataBinding(dataBinding => dataBinding.Server()
.Insert("PlayerAdd", "Player", new { })
.Update("PlayerUpdate", "Player", new { })
.Delete("PlayerDelete", "Player", new { })
)
.Columns(columns =>
{
columns.Bound(b => b.Name);
columns.Bound(b => b.Price);
columns.Command(commands =>
{
commands.Edit();
commands.Delete();
}).Width(200);
})
.Editable(editing => editing.Mode(GridEditMode.PopUp)))
model:
public class GamePlayer2
{
public int Id { get; set; }
public string Name { get; set; }
[DataType(DataType.Currency)]
public decimal Price{ get; set; }
}
Scripts are registered on _Layout. Where is a problem? Why currency(integer,date etc) textbox won't display for fields?
Make sure you have added the EditorTemplates under the Shared/EditorTemplates folder. Basically they are automatically added if you have used the wizard to create your project. If not create a demo project and copy-paste them from there.

Ajax Binding Telerik MVC Grid with DetailView in one call

I have the following markup.
#(Html.Telerik().Grid(Model)
.Name("Grid")
.DataKeys(keys => keys.Add(key => key.Id))
.Columns(columns =>
{
columns.Bound(c => c.FullNameWithEmail).ClientTemplate("<#= FullNameWithEmail #>").Title("Name and Email").Width(230);
columns.Bound(c => c.Notes);
})
.ClientEvents(events => events.OnRowDataBound("grid_onRowDataBound"))
.DetailView(checkInAppGridDetails => checkInAppGridDetails.ClientTemplate("<# if (RelatedCount > 0) { #>" +
Html.Telerik().Grid<ViewModel>()
.Name("GridDetails_<#= Id #>")
.Footer(false)
.Columns(columns =>
{
columns.Bound(c => c.FullNameWithEmail).ClientTemplate("<#= FullNameWithEmail #>").Title("Name and Email").Width(225);
columns.Bound(c => c.Notes);
columns.Bound(c => c.Actions).ClientTemplate("<#= Actions #>").Width(150);
})
.ClientEvents(events => events.OnRowDataBound("GridDetails_onRowDataBound"))
.DataBinding(dataBinding => dataBinding.Ajax()
.Select("GetRelated", "Controller", new
{
id = #ViewBag.EventKey,
ticketId = "<#= Id #>"
}))
.ToHtmlString() +
"<# } #>"
))
)
What i have here is that i am binding the main grid with Ajax call, and once rows got bound the details view gets bound with the DataBinding ajax call.
I already have in the Model a collection for the related records i wanted to show in the DetailView, i don't want the extra call to the server.
here is an example of the ViewModel
public class ViewModel
{
public string FirstProperty {get; set;}
.
.
.
public IEnumurable<ViewModel> RelatedRecords { get; set; }
}
Any idea how to bind the whole Grid with the DetailView with only single Ajax request?
Just used telerik support example to fix this, and it worked very well.Telerik Post

Resources