Asked by:
Show/Hide Div from Controller?

Question
-
User210306721 posted
I have a div on my cshtml like this:
<div id="FileContainer"></div>
And i have two Actions where i should show or hide that div:
public ActionResult Item(int? Id) { NotificationViewModel viewModel = new NotificationViewModel(); viewModel.ShowDetails = false; etc... public ActionResult Add() { NotificationViewModel viewModel = new NotificationViewModel(); viewModel.ShowDetails = true; etc...
In the Model:
public bool ShowDetails { get; set; }
so i added somethin like this:
@if (Model.ShowDetails) { <div id="FileContainer"></div> }
But i get Error 500. (Must be null i guess).
That syntax is correct?
Wednesday, April 22, 2020 5:53 PM
All replies
-
User753101303 posted
Hi,
And your action ends with View(viewModel); or just return View(); ?
Wednesday, April 22, 2020 6:05 PM -
User210306721 posted
Both uses "return View("Item", viewModel);"
Wednesday, April 22, 2020 6:07 PM -
User753101303 posted
Error 500 means you have a server side exception which should be written by default to the Windows event log. Have a look at which error you have soi that we better know what to look for in your code.
Wednesday, April 22, 2020 6:58 PM -
User475983607 posted
But i get Error 500. (Must be null i guess).Maybe but doubtful. Null exceptions are usually handled and returned to the browser.
A 500 error means the application and server were unable to recover. Check the event log for errors and consider adding try...catch blocks to your code.
Wednesday, April 22, 2020 7:02 PM -
User210306721 posted
Yeah, error looks like null.
My controller should assign the value i dont know why is lost.
if i try by js like this:
if(@Model.ShowDetails.ToString().ToLower() === false) { document.getElementById('FileContainer').style.display = "none"; }
it returns a "False" so i noticed that this is a better way to handle it casting to "false" but still not taking the true from the controller.
Wednesday, April 22, 2020 7:14 PM -
User210306721 posted
I found the issue, looks like i was always calling Action Edit instead of Add and i didnt noticed that becouse int can be null too.
But js syntax works fine.
Thanks !!
Wednesday, April 22, 2020 7:32 PM -
User475983607 posted
becouse int can be null too.No. An int defaults to zero. An int? can be null though.
Wednesday, April 22, 2020 8:14 PM -
User665608656 posted
Hi, MVC_user
I am able to show and hide div according to your code.
You can provide more code.
The following is the result of my test.
Best Regards,
YongQing.
Thursday, April 23, 2020 8:34 AM