Declare a local variable at the beginning of the View: @{ var yourStyle = “”; if(Model.Condition) { yourStyle = “float:left;”; //specific conditions and logic } else { yourStyle = “float:right;”; //any other conditions and logic } } and then just use it in your div: <div style=”@yourStyle”>
Tag: mvc
How to remove searching, filtering, ordering and info from Asp.NET MVC Datatable
var table = $(“#datatable”).DataTable({ “paging”: false, “ordering”: false, “searching”: false, “info”: false });
How to make a checkbox or dropdown readonly in Razor
You can add @disabled = “disabled” to the last parameter named HtmlAttributes. @Html.CheckBox(“someNameForYourCheckBox”, CheckedValue (True or Not), new { @disabled = “disabled” }) @Html.DropDownList(“someNameForYourDropDown”, YourSelectList (comes from Model), new { @disabled = “disabled” })
How to put text inside MVC Razor code block
The contents of a code block ({ … }) are expected to be code or markup (tags), not plain text. If you want to put text directly in a code block, you have three choices: Wrap it in any HTML tag Wrap it in the special Razor <text> tag, which will just render the text without the […]
Populating Javascript array with Model in ASP.Net MVC Razor
We just need to loop through the razor collection to populate the Javascript array as below: @foreach (var item in Model) { @:$(function () { @:$(“#map”).googleMap(); @:$(“#map”).addMarker({ @:coords:[@item.Latitude], @:title: `@item.City, @item.District`, @:text: `@item.Address` @:}); @:}); }
Pass model from Razor view to JavaScript
Let’s say you send a model called “MyModel” from controller to view. In Controller: … var result = new MyModel() { Id = 1, Name = “Ercan”, FavoriteTeam = “Galatasaray”, FavoriteGame = “Half Life Series” } return View(result); … and now we will send this model which comes with result to Javascript via setName function: […]
How to add placeholder to Multiple Selection DropDownList in Asp.Net MVC
@Html.ListBox( “Countries”, ViewBag.AllCountries as MultiSelectList, new { @class = “form-control”, data_placeholder = “Choose a Country…” } )
How to render HTML tags Asp.Net MVC
string myValue = “<b>Hello</b>”; @Html.Raw(HttpUtility.HtmlDecode(myValue))
How to refresh a DIV content with Javascript
To reload a section of the page, you could use jquerys load with the current url and specify the fragment you need, which would be the same element that load is called on, in our below case #myDivId function updateDivContent() { $(“#myDivId”).load(window.location.href + ” #myDivId” ); }
Creating Multiline textbox using Html.Helper function in Asp.Net MVC
@Html.TextArea(“Body”, null, new { cols = “55”, rows = “10” }) or
Default Settings for Datatable in MVC
When working with DataTables over multiple pages it is often useful to set the initialisation defaults to common values (for example you might want to set dom to a common value so all tables get the same layout). This can be done using the $.fn.dataTable.defaults object. This object will take all of the same parameters as the DataTables initialisation […]
How to assing Selected Value to DropDownList in Asp.Net MVC
IEnumarable SelectList’s 4th parameter is just for that: @Html.DropDownList(“AuthorID”, new SelectList(Model.Authors, “Id”, “AuthorFullName”, Model.Books.AuthorId), “—Select an Author—“, new { @class = “form-control” })
How to use Html.TextBox for Date in Asp.Net MVC
@Html.TextBox(“BookName”, Model.Books.BookName, new { @class = “form-control”, @type = “date” })
Formatting a Nullable DateTime with ToString() in ASP.Net MVC
Let’s say we have a Nullable DateTime named StartTime [DisplayFormat(DataFormatString = “{0:dd.MM.yyyy}”)] public Nullable<System.DateTime> StartTime { get; set; }
How to apply a custom CSS class to Devexpress components
For all the Devex Components, you can use ControlStyle > CssClass property to apply your custom CSS class (Example 1 and 2) or you can do it with parameter (if it is allowed – as in Example 3). Here is 3 examples for MVC: @Html.DevExpress().SpinEditFor(m => m.Books.Rating, settings => { settings.Properties.SpinButtons.ShowIncrementButtons = true; settings.Properties.Increment = […]
@helpers in Asp.NET MVC Razor
A @helper in Razor ultimately defines a function you can invoke from anywhere inside the view. The function can output literal text and can contain code blocks and code expressions. Don’t forget that heavy amounts of code in a view can lead to grief. You have @PluralPick(Model.Count, “child”, “children”). @helper PluralPick(int amount, string singular, string […]
Using Code Blocks, Mixing Text & C# Code and Comments in Asp.NET MVC Razor
In Razor you can create blocks of code nearly anywhere using @{ }. A block of code doesn’t emit anything into the output, but you can use the block to manipulate a model, declare variables, and set local properties on a view. Be extremely careful about having too much code in a view, however, because […]
Using View Model to pass Multiple Models in Single View in MVC
In MVC we cannot pass multiple models from a controller to the single view. There are a few solution for that problem. But in this article we will use View Model. Here is the solution: