Posts

Showing posts with the label Asp.Net Mvc

Change Kendo Grid Datasource Use JS

Answer : I think you should first create a new DataSource (see https://demos.telerik.com/kendo-ui/datasource/remote-data-binding for remote data) var dataSource = new kendo.data.DataSource({ data: [ { name: "John Doe", age: 33 } ] }); And then append it to the grid by using the setDataSource method (https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/setdatasource) var grid = $("#grid").data("kendoGrid"); grid.setDataSource(dataSource); Since you want to change the action for your read then you can just do that. According to this question you could just set the dataSource Read url and refresh your grid data with something like that: var grid = $("#grid").data("kendoGrid"); grid.dataSource.transport.options.read.url = "newUrlPath"; grid.dataSource.read(); grid.refresh(); If you don't actually want to change your dataSource but your data and possibly get your list of items from some aj...

Can We Scaffold DbContext From Selected Tables Of An Existing Database

Answer : One can solve the problem by usage of dotnet ef dbcontext scaffold command with multiple -t ( --table ) parameters. It allows to specify all the tables, which needed by imported (scaffolded). The feature is described initially here. It is possible to specify the exact tables in a schema to use when scaffolding database and to omit the rest. The command-line examples that follow show the parameters needed for filtering tables. .NET Core CLI: dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila -t actor -t film -t film_actor -t language -f Package Manager Console in Visual Studio: Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir Sakila -Tables actor,film,film_actor,language -f Force tag will update the existing selected models/files i...

Asp.net Mvc How To Add Placeholder For Html.dropdownlist

Image
Answer : You could try this: @Html.DropDownList("country", new SelectList(ViewBag.countries), "-select- ", new { @class="chzn-select", @style="width:160px;" }) In your collection ViewBag.Countries just insert a dummy record at the from of the collection with the name "-select-". You should be able to force the selected item with an alternate constructor like this: @Html.DropDownList("country", new SelectList(ViewBag.countries as System.Collections.IEnumerable, "name", "name", "-select-"), new { @class="chzn-select", @style="width:160px;" } ) A quick and (not so) dirty solution involving jQuery. Instead of adding a dummy item at the start of the list, prepend a new option that is disabled. The main advantage is that you don't have to mess with a dummy item in your list , and most important, you won't be able to select that dummy item in the page : ...

ASP.NET MVC 4 - Cannot Perform Runtime Binding On A Null Reference

Answer : OK I am posting the full answer here - Try @ before if(@ViewBag.Stats[index] == null){ and remove @ from @ViewBag inside the if so that it look like this - @if(ViewBag.Stats[index] == null){ You are setting index = 0 , inside foreach , so it is initialised in every loop. Initialise it outside foreach like this var index = 0; foreach ... if you are facing problem for the scope try this - @{ var index = 0; foreach (....) { ....... index++ } }
Answer : It shows that the assembly you referenced in the project has different version(4.0.0.1) as what you have in web.config(4.0.0.0). Please check that the referenced assembly for System.Web.Mvc is the same as written in the web.config. If not then add reference to the appropriate assembly. Right click References -> Add Reference -> ... Install Nuget Package Microsoft.AspNet.Mvc for all the project referencing System.Web.Mvc dll Example: Install-Package Microsoft.AspNet.Mvc

ASP.NET Core MVC Mixed Route/FromBody Model Binding & Validation

Image
Answer : Install-Package HybridModelBinding Add to Statrup: services.AddMvc() .AddHybridModelBinder(); Model: public class Person { public int Id { get; set; } public string Name { get; set; } public string FavoriteColor { get; set; } } Controller: [HttpPost] [Route("people/{id}")] public IActionResult Post([FromHybrid]Person model) { } Request: curl -X POST -H "Accept: application/json" -H "Content-Type:application/json" -d '{ "id": 999, "name": "Bill Boga", "favoriteColor": "Blue" }' "https://localhost/people/123?name=William%20Boga" Result: { "Id": 123, "Name": "William Boga", "FavoriteColor": "Blue" } There are other advanced features. You can remove the [FromBody] decorator on your input and let MVC binding map the properties: [HttpPost("/test/{rootId}/echo/{id}...

Checkbox Disabled Attribute In ASP.NET MVC

Answer : It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox. <input type="checkbox" disabled> <input type="checkbox" disabled="disabled"> <input type="checkbox" disabled="false"> <input type="checkbox" disabled="no"> <input type="checkbox" disabled="enabled"> This should work in the razor. Simple If condition and rendering what you want. @if(item.Selected) { @Html.CheckBoxFor(modelItem => item.Selected) } else { @Html.CheckBoxFor(modelItem => item.Selected, new { @disabled = "disabled"}) } You may consider writing a custom html helper which renders the proper markup for this. This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a @disabled property when it should be d...

ASP.NET MVC: No Parameterless Constructor Defined For This Object

Answer : I just had a similar problem. The same exception occurs when a Model has no parameterless constructor. The call stack was figuring a method responsible for creating a new instance of a model. System.Web.Mvc.DefaultModelBinder. CreateModel (ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) Here is a sample: public class MyController : Controller { public ActionResult Action(MyModel model) { } } public class MyModel { public MyModel(IHelper helper) // MVC cannot call that { // ... } public MyModel() // MVC can call that { } } This can also be caused if your Model is using a SelectList, as this has no parameterless constructor : public class MyViewModel { public SelectList Contacts { get;set; } } You'll need to refactor your model to do it a different way if this is the cause. So using an IEnumerable<Contact> and writing an extension method that creates the ...

Call Custom Confirm Dialog In Ajax.Beginform In MVC3

Answer : I came across this to customize AjaxOptions Confirm text with a value that has not been created yet when Ajax.Beginform is rendered . For example: Confirm="Are you sure you want to create Customer Id" + someValue + "?" Finally a found a solution: The approach is regarding a change in submit button behavior with JQuery to pull the value, run my own Confirm dialog and submit the Ajax form if user confirm. The steps: 1- Remove Confirm from AjaxOptions and avoid set button's type="submit", could be type="button" <div> @using (Ajax.BeginForm("Function", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "theForm", InsertionMode = InsertionMode.Replace, LoadingElementId = "iconGif", OnBegin = "OnBegin", OnFailure = "OnFailure", OnSuccess =...

Can You Just Update A Partial View Instead Of Full Page Post?

Answer : Not without jQuery. What you would have to do is put your Partial in a div, something like: <div id="partial"> @Html.Partial("YourPartial") </div> Then, to update (for example clicking a button with the id button ), you could do: $("#button").click(function () { $.ajax({ url: "YourController/GetData", type: "get", data: $("form").serialize(), //if you need to post Model data, use this success: function (result) { $("#partial").html(result); } }); }) Then your action would look something like: public ActionResult GetData(YourModel model) //that's if you need the model { //do whatever return View(model); } Actually, if your Partial has a child action method, you can post (or even use an anchor link) directly to the child action and get an Ajax-like affect. We do this in several Views. The syntax is @Html.Action...

Clear Dropzone.js Thumbnail Image After Uploading An Image

Answer : You can try this: myDropzone.on("complete", function(file) { myDropzone.removeFile(file); }); More information here: http://www.dropzonejs.com/#dropzone-methods removeAllFiles() and removeFile() will trigger the server-side removal too, if you hooked Dropzone to remove files as well. The solution to clear it only client-side, remove the file preview, and if you had a blank state message, remove the dz-started class to prevent the Dropzone CSS from hiding it: $('.dropzone')[0].dropzone.files.forEach(function(file) { file.previewElement.remove(); }); $('.dropzone').removeClass('dz-started'); Another option (similar to answer of Giraldi, but then when all files are completed): myDropzone.on("queuecomplete", function () { this.removeAllFiles(); });