ViewBag return difference between input and label - asp.net-mvc

In Controller
public IActionResult List()
{
ViewBag.SomeThingERR = true;// something bool value
return View();
}
In view
<input type="text" id="SomeThingERR2" value="#ViewBag.SomeThingERR" />
<label>SomeThingERR:</label> #ViewBag.SomeThingERR
Result
input = value
lable = True?
I don't know why it difference value

As everything rendered in the view is text, try to convert non-string entities into a string explicitly.
<input type="text" id="SomeThingERR2" value="#(ViewBag.SomeThingERR.ToString())" />
<label>SomeThingERR:</label> #(ViewBag.SomeThingERR.ToString())

If you just want to print the contents of the ViewBag, you can do so.
Controller:
public IActionResult Index()
{
ViewBag.SomeThingERR ="true";// something bool value
return View();
}
View:
<input type="text" id="SomeThingERR2" value=#ViewBag.SomeThingERR />
<label>SomeThingERR:</label> #ViewBag.SomeThi
If you have a logical judgment, you can refer to this article:Mvc ViewBag - Cannot convert null to 'bool' because it is a non-nullable value type

Related

How to get value of Checkbox using formcollection in MVC4

I have a function, that collect the value of the controls of some view(.cshtml)
private string ExtractEmailId(FormCollection form)
{
var value = form["CkbQuestion1"];
return value;
}
I am receiving value of Checkbox as "true,false". I need the value of that control.
How I can have that?
Any Idea please.
I expect you required the below code. Please replace your code with this.
[HttpPost]
public string ExtractEmailId(FormCollection form)
{
var value = form["CkbQuestion1"];
return value;
}
And you view will be similar to
#Using(Html.Beginform("ExtractEmailId"))
{
<input type="checkbox" name="CkbQuestion1" />
<input type="submit" value="Submit" />
}
CkbQuestion1 must be set as value of name attribute for checkbox. It will look like this in your Html.
<input type="checkbox" name=""/>
it will return Empty string if you didn't define the value of value attribute. If you don't define value attribute then it will post the NULL to server.
if you have property for that field than simply change your mark up and write
#Html.EditorFor(td => td.PropName)
and now you can access your checkbox value in controller.

Why ViewContext.RouteData.Values["X"] return key vlaue ? while I expect just the value

I expect in Razor the #ViewContext.RouteData.Values["id"] return an integer , While it return 'Id=X'.
for instance my controller like :
public ActionResult Index(int? id)
{
return View();
}
And in Razor :
<input type="text" value='#ViewContext.RouteData.Values["id"]' id="routeDataId" />
the result is a Textbox with the value of 'Id=X'
I expect in Razor the #ViewContext.RouteData.Values["id"] return an
integer , While it return Id=X.
Returning Id=X by RouteData.Values["id"] is correct behaviour, according to MSDN.
If you want to get a value of that object, you could use:
#ViewContext.RouteData.Values["id"].ToString()
So, your input should look like:
<input type="text" value='#ViewContext.RouteData.Values["id"].ToString()' id="routeDataId" />
If this also won't work, you could do that trick:
public ActionResult Index(int? id)
{
if(id.HasValue)
TempData["id"] = id;
else
TempData["id"] = "empty value";
return View();
}
and then:
<input type="text" value='#TempData["id"]' id="routeDataId" />
You should change quotes from ' to " like this:
<input type="text" value="#ViewContext.RouteData.Values["id"].ToString()" id="routeDataId" />

How to apply BindAttribute.Prefix to an action method parameter of type Int32?

When I apply BindAttribute.Prefix to an action parameter value, I would expect DefaultModelBinder to either use the custom prefix or fall back to an empty prefix ( in either case value parameter would be assigned number 100 ). But instead it does neither and as such the following code throws an exception:
The parameters dictionary contains a null entry for parameter 'value' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)'
[HttpPost]
public ActionResult Index([Bind(Prefix = "originalPrefix")]int value)
{
return View();
}
public ActionResult Index()
{
return View();
}
Index.cshtml:
using (Html.BeginForm())
{
<input id="originalPrefix.value" name="originalPrefix.value" type="text" value="100" />
<input id="value" name="value" type="text" value="100" />
<input type="submit" value="submit" />
}
a) Why isn't DefaultModelBinder able to bind form data to value parameter?
b) If possible, how do we apply BindAttribute.Prefix to an action method parameter of type Int32?
thank you
The value of the name attribute should be "originalPrefix" not "originalPrefix.value". You're trying to bind the parameter and not the value property of the parameter.

string Model, passing value to controller issue

I have a ASP.NET Razor view which binds to string. Its very simple:
#model string
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Hello, #Model
#using(Html.BeginForm())
{
<fieldset>
<label for="name" style="color: whitesmoke">Name:</label>
<input type="text" id="name"/>
<br/>
<input type="submit" value="Submit"/>
</fieldset>
}
And a simple controller:
[HttpGet]
public ActionResult Index()
{
object model = "foo";
return View(model);
}
private string name;
[HttpPost]
public ActionResult Index(string name)
{
return View();
}
When I push the submit button, the Index Post action result triggers, but the 'string name' parameter is null. Isn't Razor smart enough to automatically bind this property to my controller from the view because the input id matches the name of the param on the controller? If not, how do I bind this? I know with a model with properties I can use Html.HiddenFor(m => m.Foo), but since there's no properties, I don't know how to call this method properly.. I can set it properly calling Html.Hidden("name","foo"), but I don't know how to pass a the value here. I know I can use jquery call such as:
#Html.Hidden("name", "$('input[id=name]').val())");
This literally sends the jquery string to the controller as the value... I'm not sure what to do at this point. Thanks!
It is smart enough to bind the property, just give your input a name which matches with the action parameter:
<input type="text" id="name" name="name" />

MVC - two text box with the same parameter

#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order")<br />
#Html.TextBox("order")
<input type="submit" value="Submit" />
}
Hello how can i use two text box and pass value to the same parameter? The first textbox work but the other textbox doesn't.
public ActionResult Orders1(IEnumerable<int> order)
{
var query = from a in context.CM_Checkout_Details
where a.CheckoutDetails_ID == order // my error
select a;
return View(query);
}
}
simple solution would be to change your parameter from int to IEnumerable<int>
public ActionResult Orders1(IEnumerable<int> orders)
{
}

Resources