ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
CssClass of Grid Column depending on AjaxRadioList value
Title:
B
I
{code}
?
I have a grid which content is dependent of the value of a AjaxRadioList. That works perfectly. What I need now is to determine the CssClass of a couple of columns depending on the value of the AjaxRadioList. Tried to use a ViewData value but I don't know how to pass the AjaxRadioList value to the `public void OnGet` method to set the ViewData value. I am using MVC @(Html.Awe().Grid("TheGridForecastKegs") .Mod(o => o.Loading(true)) .Url(Url.Action("GetForecastKegs", "Forecast")) .Columns( new Column { Bind = "Id", Hidden = true }, ... )
Save Changes
Cancel
AM
asked at 15 Dec 2022
Answers
B
I
{code}
?
you could have the grid html returned by a controller's action: public IActionResult Grid1(int? val1) { return PartialView("Grid1", new MyModel{ Color = val1 > 3 ? "blue" : "green" }); } Grid1.cshtml: @model MyModel @(Html.Awe().Grid("TheGridForecastKegs") .Columns( new Column { Bind = "Id", Hidden = true, CssClass = Model.Color }, ... ) Main view: <div id="grid1placeh"> @Html.Action("Grid1", new { val1 = initVal1Value }) </div> <script> $(function(){ $('#RadioList1').on('change', function(){ var rval = $(this).value(); $.get('@Url.Action("Grid1", { val1: rval }, function(res){ $('#grid1placeh').html(res); }); }); }); </script>
Save Changes
Cancel
Omu
answered at 16 Dec 2022
Thanks for your response... I guess that your solution would work if all columns needed to change, but in my case I only need specific columns depend on their drop down values and I don't know which column it is, sometimes if the second column, sometimes is the third, etc. It is just data driven.
at 19 Dec 2022
AM
To give you some context: I use the spreadsheet type of grid for the users to input forecast amounts but not I need to block forecast columns depending on a database table.
at 19 Dec 2022
AM
so since you have a db table instead of dropdowns/radiolist, as an if condition value, just use those values in the view instead, the solution stays the same, I presume you don't need the js part now if there's no user input, user that clicks on something and a column changes color
at 19 Dec 2022
Omu
Hi, I have 2 radiolists that drive which values to show in the grid. Those are setup as ".Parent" in the Grid. When they are selected, the "public IActionResult" gets called without any issues; it goes to the db and get the data which is binded to the grid columns. This method also calls the db to determine if certain columns should be read only; just need tell the grid to make the columns readonly. I thought of using ViewDatas but even though they are setup properly their values don't get back to the grid when it is updated with the data based on the selected radiolists. Makes sense?
at 19 Dec 2022
AM
is it possible to make the viewdata be updated by the "public IActionResult" programatically based on my DB values?
at 19 Dec 2022
AM
yes, you can put any data regardless of the source db/memory/file, besides viewdata you can also use a strongly typed model to pass data from controller to view, we can recommend you to watch this tutorial: https://youtu.be/fsfOFL4bXXA
at 19 Dec 2022
Omu
I have seen this video before. I think you are not getting what I need or I don't understand what you are trying to tell me. I need a way to have the "public IActionResult" triggered by the grid to update the DataView values. I am using the code you can see above to return the "Json(gridModel)" with he grid contents. This method also knows which columns to make readonly. So what do I need to do so the binded columns to have a cssclass that is depending on the values from the database. I wish that I could share the code to explain what I need
at 20 Dec 2022
AM
When I update the ViewData within the "public IActionResult" method triggered by the grid when the parents of the grid get updated (they being the radiolists), I can see the ViewData values are updated but they don't reflect. I have one ViewData for each column to determine if it is write or read.
at 20 Dec 2022
AM
This is part of the method showing how the ViewData is set as an example where the brandFilter and the provinceFilter are the values form the radiolists (ugly indenting to fit your comment) IQueryable<ForecastColumnLockedClass> forecastHeaderLock = from p in _context.ForecastColumnsLocked where p.BrandID.ToString() == brandFilter && p.Province == provinceFilter select p; ViewData["LockClass"] = (forecastHeaderLock.First().LockColumn == 0) ? "forecaststyle" : "lockedstyle"; ViewData["LockFunc"] = (forecastHeaderLock.First().LockColumn == 0) ? "txt" :
at 20 Dec 2022
AM
And this is the column in the grid: @(Html.Awe().Grid("TheGridForecastKegs") .Mod(o => o.Loading(true)) .Url(Url.Action("GetForecastKegs", "Forecast")) .Columns( new Column { Bind = "Forecast", Width = 75, Header = ViewData["ForecastTitle"].ToString(), CssClass = ViewData["LockClass"].ToString(), ClientFormatFunc = ViewData["LockFunc"].ToString() }, ... How can I get those 2 ViewData values updated when "public IActionResult GetForecastKegs(GridParams g, string brandFilter, string provinceFilter, string productFilter)" gets called
at 20 Dec 2022
AM
The method triggered by the Grid returns `Json(model)`, and setting `ViewData` in it won't do anything, because there's no view. `ViewData` is used when you return `View` or `PartialView`. The method is called by js to get data (json). The view that holds the grid code/markup is not executed.
at 20 Dec 2022
Omu
So you need to change css class depending on the values, is it depending on an aggregate value for each column ? or do you want to change the class for each individual grid cell ? You can try to put your `ViewData` setting code in the method that returns the View that renders the `Grid`, if that make sense.
at 20 Dec 2022
Omu
Thanks for your patience. It is the column. I guess the issue is that the method that renders the grid doesn't know at the time the AjaxRadioLists (x2) values. The one that knows the value is the iActionResult. I set the ViewData inside this method but I am not sure how to return to the view.
at 21 Dec 2022
AM
Please try to do this: https://www.aspnetawesome.com/learn/mvc/CommonProblems#Isolate-the-problem so we could have a look
at 21 Dec 2022
Omu
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK