The null attribute is not executed in MVC 5. What kind of magic is this?

<form name="mailform" id="mailform" method="post" action="@(ViewBag.Test)" class="testForm">

What I do is that this will give the action attribute as action=""(in case ViewBag.Test = null), but I am confused about the behavior of ASP.NET MVC.

If ViewBag.Test is not defined than my action attribute, this is not rendering. To confirm, I open the View-source in Chrome and Firefox, and none of them have an action attribute for my form. I also check this by modifying the text, but the action attribute remains not rendering.

Can someone explain what it is? And the link I can check is the general behavior of ASP.NET MVC 5

+4
source share
3 answers

MVC- (, , Razor), . " " , -, MVC 4. . (: ), .

, , , , , , , .

, . , ...

<form name="mailform" id="mailform" method="post" action="@(ViewBag.Test ?? "")" class="jeetForm">
+4

, MVC 5 .

@using (Html.BeginForm()) {}

Null string.empty , .

ASP.NET MVC. .

+1

You must handle null values โ€‹โ€‹out.

Just check the box after zero or empty or free space.

@{ var link = !string.IsNullOrEmpty(ViewBag.Test) ? ViewBag.Test : string.Empty;
  var isSet = !string.IsNullOrEmpty(ViewBag.Test);
}

And use isSet and the link for your logic.

Null is not converted by anything. Razor, string.Empty is something else (it will display the attribute specified in your case).

0
source

All Articles