How to Send Additional Data in Kendo Grid's AJAX Create - asp.net-mvc-4

I am able to send additional data to a Kendo grid's AJAX Read action using a pattern like so...
#(Html.Kendo().Grid<ModelClass>()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Read", "Controller").Data("getAdditionalData"))
function getAdditionalData() {
return {
AdditionalData: 'data'
};
}
public ActionResult Read([DataSourceRequest] DataSourceRequest request, String additionalData)
{
}
... but how does one send additional data to the Create action? In my scenario, creation requires information that is not entered by the user, but is available both in the view and as the return from getAdditionalData(). I've tried passing it using the following, but it is not being received by the action method.
.Create(create => create.Action("Create", "Controller").Data("getAdditionalData"))
public ActionResult Create([DataSourceRequest] DataSourceRequest request, ModelClass model, String additionalData)
How is this done? Or how else can additional data be passed to the AJAX create action?
Ref Docs:
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding#pass-additional-data-to-the-action-method
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-editing
Update with Solution:
As it happens, there cannot be a field name (e.g. "additionalData") duplication between the grid's view model (e.g. "ModelClass") and the client-side data model. That was not apparent in the question as I sanitized the code to post. Not sure if this is happening at the javascript, MVC or Telerik layer.
The solution was to change the client-side data model's field name to be different that the field name in the grid's view model.
Update with Another Solution:
Another option is to set the default value of the field in the grid's view model for create operations like so:
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => {
model.Field(x => x.Id) // Set default value for grid model field
.DefaultValue(Model.DefaultId); // as provided by controller in view model.
})

I've got the same problem and I resolve it.
public ActionResult Create([DataSourceRequest] DataSourceRequest request, ModelClass model, String additionalData)
in this line, change String for string and additionalData for AdditionalData, the same word you have in your js.
One more things, in the clause .Events of DataSource, don't put at the end .RequestEnd("getAdditionalData")
Normally it will work.
Hope it helps!

Related

How to add initial filter to BootstrapGridView in razor

When the page loads, I need to already have a filter in place for a given column.
Here is a sample:
#(Html.DevExpress()
.BootstrapGridView<SomeViewModel>("SomeName")
... //other settings
.Columns(columns =>
{
columns.AddCommandColumn()
.ShowSelectCheckbox(true)
.SelectAllCheckboxMode(GridViewSelectAllCheckBoxMode.AllPages);
... //some other columns
columns.Add(m => m.SomeBoolProperty).Caption("SomeCaption");
})
... //other settings
)
Ok, now I need the column of "SomeBoolProperty" to have a filter already in place when the grid loads (let's say I want to have only "checked"/true entries to appear in the grid - the model should still have them all, obviously, otherwise I'd do it in the controller). Can I do that in razor somehow? From what I've seen I can change filter templates but can I actually physically add a filter? If not how else could I do this, maybe in javascript?
Try using the SettingsHeaderFilter property, you can add filtering mode to the column header, if your column is boolean type, then GridHeaderFilterMode.CheckedList will solve in this case.
columns.Add(m => m.SomeBoolProperty)
.Caption("SomeCaption")
.SettingsHeaderFilter(settings => settings
.Mode(GridHeaderFilterMode.CheckedList));
Documentation: Header Filter - Checked List mode
Found a way in the end, like this:
#(Html.DevExpress()
.BootstrapGridView<SomeViewModel>("SomeName")
... //other settings
.OnInitialized((s, e) =>
{
s.AutoFilterByColumn(s.DataColumns["SomeBoolProperty"], "true");
})
... //other settings
.Columns(columns =>
{
columns.AddCommandColumn()
.ShowSelectCheckbox(true)
.SelectAllCheckboxMode(GridViewSelectAllCheckBoxMode.AllPages);
... //some other columns
columns.Add(m => m.SomeBoolProperty).Caption("SomeCaption");
})
... //other settings
)
By adding OnInitialized.

How to use a single form to collect data for two or more models in Yii2?

My problem is to dynamically create forms through jquery.
The user should be able to dynamically generate inputs such that he can save multiple rows into the corresponding model/table in one go.
Now I don't know how do I generate the name attribute for the forms for multiple models. I suppose it should be something like ModelName[Property][] (but I would preferably want to do it in 'Yii way' instead of hardcoding the names)
To understand this better, Here I found a similar post in Yii Wiki.
using a single form to collect data for two or more models
How can this be modified for Yii2? So that the user should be able to fill in data for all the (dynamically generated) rows and submit them in one go.
Tutorial of tabular form for multiple models.
Try this, Reference
public function actionCreate()
{
$user = new User;
$profile = new Profile;
if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) {
$user->save(false); // skip validation as model is already validated
$profile->user_id = $user->id;
$profile->save(false);
return $this->redirect(['view', 'id' => $user->id]);
} else {
return $this->render('create', [
'user' => $user,
'profile' => $profile,
]);
}
}

Why sometime dropdownlist value can not be retrieved in model

I have a form that use DropDownListFor that are set in mvc model. But strangely enough, the current value of them sometimes can not be retrieved in model (sometimes they can). The code is,
<% using (Html.BeginForm("EditPageSubmit", "Home", FormMethod.Post))
{
<%: Html.DropDownListFor(model => model.Destination, Model.Destinations, Model.Destination)%>
...
model.Destinations= (from r in this._repository.Destinations
select new SelectListItem
{
Text = r.Name,
Value = SqlFunctions.StringConvert((double)r.DestinationID),
Selected = false
});
Model.Destination = "...";
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditPageSubmit(FormCollection collection)
{
var updatedModel = new SalesViewModel();
if (TryUpdateModel(updatedModel))
...
}
Strangely enough, sometimes (often when dropdownlist is not changed and remains with initial value), updatedModel.Destination is null, and sometimes it is the selected value. I have really no idea on this problem and need help on it. Thanks.
I find the reason. The initialization variable can not be the same as model id. In this case, simply change to Html.DropDownListFor(model => model.Destination, Model.Destinations, Model.Destination1) will work.

Binding Gridview with drop down list selection MVC 5

I have a drop down list (Cities) and on selection it should be able to bind a Grid View in MVC 5
i am able to bind the drop down list and grid view but unable to handle the select event of Drop Down list. I dont know how to proceed on this I dont want to use Jquery. Below is my code
// BELOW IS THE DROP DOWN LIST CODE
#using (Html.BeginForm("BindGridView", "FacebookConfig", FormMethod.Post, new { id = "Id" }))
{
#Html.DropDownList("Cities")
}
// ON SELECTION OF ABOVE I SHOULD BE ABLE TO BIND THE BELOW GRID VIEW
#if (Model != null)
{
foreach (var item in Model)
{
#Html.DisplayFor(modelItem => item.Keywords)
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
// BELOW IS THE CODE IN CONTROLLER
public ActionResult Index()
{
var cities = So.BL.City.GetCities();
ViewBag.Cities = new SelectList(cities, "Id", "Name");
return View();
}
[HttpPost]
public ActionResult BindGridView(int id)
{
var filter = So.BL.FacebookFilter.SelectFBApprovalFilters(id);
return View(filter);
}
If you want to perform an operation on dropdown on change event you must go for javascript/jquery for handling the element events. To satisfy your requirement you may do as,
Create a <div> to hold the new grid view to be created with the id to select.
Create a partial view that generate the grid view.
Create a function either in jquery/javascript for dropdown list on change event.
From the function call an action method using ajax.
Create an action method which would do what ever the functionality you want to do and return the partial view with the model required for generating the grid view.
Finally on success method of ajax call, write the output response to the inner html of the <div>.
If definitely not going for jquery or javascript then do a post back and from the selected dropdown value in the post method fetch the data for grid view and bind it to the grid. Note here the form submission will not occur on dropdown selection must use a submit button. Also show the grid based Model if not null.
Choose which one you likely to adopt.

CGridview filter on page load with pre define value in search field

I am working with the Yii framework.
I have set a value in one of my cgridview filter fields using:
Here is my jQuery to assign a value to the searchfield:
$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');
And here my PHP for calling the cgridview:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
'class'=>'AjaxList',
'maxButtonCount'=>25,
'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()." ".CHtml::link($xcel.' Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));
The value appears in the search field and my cgridview works correctly without any issues, but I am unable to trigger the cgridview to refresh or filter. Does anyone know who to trigger the cgridview to filter after page load with a predefined value?
Any help would be greatly appreciated and please let me know if you need additional information.
Thank you.
You can solve the problem without any clientside code modification. In your controller action just set the default value for the attribute as shown below
public function actionAdmin()
{
$model = new Bills();
$model->unsetAttributes();
$model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('admin',array('model'=>$model));
}
Have a look at 'default' index action that gii generates:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('index',array('model'=>$model));
}
So if you add one line like: $model->attribute = 'test';, you're done. 'attribute' is of course the attribute that has to have the default filter value (in this case value is 'test') :). So your code looks like:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
if(!isset($_GET['Bills']['attribute']) {
$model->attribute = 'test';
}
$this->render('index',array('model'=>$model));
}
Of course youre attribute will have a test value (in filter) set up as long as you wont type anything in its filter field. I hope that that's what you're looking for. Your filter should work as always.
Sorry for my bad english :)
Regards
You can use Yii's update:
$.fn.yiiGridView.update('bills-grid', {
type: 'GET',
url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
success: function() {
$.fn.yiiGridView.update('bills-grid');
}
});
This is how i do it, just change the URL, it should be the same controller action of the gridview and change URL parameters to the structure represented in there, should be like Bills[attribute]=value.