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.
Related
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
I need to pass which button is clicked for the form submit. But the hidden input is not received in the controller. Below are my code snippets
View Model:
public class DocumentViewModel
{
public int Id { get; set; }
public int ActionId { get; set; }
}
razor(cshtml):
<form....>
<input id="docActionId" name="docActionId" asp-for="ActionId" type="hidden" value="initialValue" />
</form>
JavaScript:
$("#save_btn").on("click", function ()
{
$("#docActionId").val("test1");
});
$("#submit_btn").on("click", function ()
{
$("#docActionId").val("test2");
});
I tried showing the current value in alert function if it is change and it did.
In my Controller/Action when I debug, I get a null value for the ActionId variable. I think this is very simple but I don't know what I did wrong.
Please help. I'm stucked in this for 2 hours now. Thanks in advance.
remove the "Id" attribute... that's all you need. "asp-for" does that. adding yours duplicates it kind
Remove the name="docActionId" html attributes, they will be generated automatically by Razor.
Specifically by the asp-for="ActionId" attribute.
Furthermore the reason this doesn't work is because the name attribute which is docActionId != ActionId which is the name of the property of your object. So it doesn't know where to bind it to.
<form id="myform">
<input id="docActionId" asp-for="ActionId" type="hidden" value="initialValue" />
<button type="submit" id="save">Submit</button>
</form>
Try this javascript perhaps the form is submitted before you attach the value
var saveButton = documnet.getElementById('save')
saveButton.addEventListener('click', function(e) {
e.stopPropagation();
documnet.getElementById('docActionId').value = "test value"
documnet.getElementById('myform').submit()
})
<input type="hidden" id="tag" >
type="hidden" didn't work for me with .net core 7 but only hidden keyword worked.
<input hidden id="tag">
I'm very new in MVC :(
I created a dynamic form by cloning a principal DIV element and its elements. The elements are combobox, textbox and a date textbox. When I create a new "clone", the DIV every member of itself has an incremental ID like tab_Container, tab_Container_1, text, text1, combo, combo1, etc... Now, I'm trying to get the values of each member in the Divs into the controller.
Googling I find something like this:
[HttpPost]
public ActionResult NewEntry(Model Entry)
{
Control myControl = new Control();
myControl.FindControl("Text0");
if (myControl != null)
{
/// apparently, find the control,here i wanna to get the value of each field !! ¿?
/// do i have to create a list[] ... exist something like Entry.Text0 = myControl.value?
}
else
{
Response.Write("Control not found");
}
return View(Entry);
}
Any suggestion? Is Control the best option? Do I have to do something else in Javascript code?
While it's normally better to have some sort of Model / ViewModel this situation is a bit different. MVC binds on the "Name" property of your form inputs.
So say for instance your razor syntax generates something like this:
<form>
<input type="text" name="input1" />
<input type="text" name="input2" />
<!-- etc etc etc -->
<input type="submit" value="submit" />
</form>
since this is dynamically generated and you don't have a model that would cleanly bind to this. You can use the FormCollection type as the parameter of your action. This gives you a collection of all items posted to the server that you could then loop through or peer into to get the properties that you want.
public ActionResult myAction(FormCollection collection)
{
var value = collection["input1"];
var value2 = collection["input2"];
return View();
}
I need to pass some flag into a view and get it back.
But requirements are:
this flag is not part of model
and it must not be stored in session or such.
I mean - flag visibility scope should be - current page for current user only.
I tried to put it into a ViewBag like this:
public ActionResult Product_Add()
{
ViewBag.IsNew = true;
ViewData["IsNew"] = ViewBag.IsNew;
ViewBag.RecordActionName = "Add";
return View("Product_Edit");
}
And then use this code in *.cshtml:
<input type="hidden" name="IsNew1" value="#ViewBag.IsNew" />
<input type="hidden" name="IsNew2" value='#ViewData["IsNew"]' />
But in resulting HTML I see this:
<input type="hidden" name="IsNew1" value="value" />
<input type="hidden" name="IsNew2" value='value' />
Then I tried this code in *.cshtml:
#{ string isNewRec = ViewBag.IsNew.ToString(); }
<input type="hidden" name="IsNew1" value="#isNewRec" />
And now it works:
<input type="hidden" name="IsNew1" value="True" />
Question is - why first attempt put "value" text instead of boolean value into HTML?
Also the strange thing - this code works fine:
<input type="hidden" name="ProductID" value="#ViewBag.ProductID" />
So, for integer values in ViewBug it works, returns an actual interger value, but for boolean it returns "value" text.
Another part of question.
Currently I use code like this:
[HttpPost]
public ActionResult Product_Commit(Product pProduct)
{
if (ModelState.IsValid)
{
AbcProducts ctx = new AbcProducts();
bool isNew = Convert.ToBoolean(Request.Form["IsNew"]);
if (isNew)
ctx.Products.Add(pProduct);
else
{
// [...]
}
ctx.SaveChanges();
}
return View();
}
Could you recommend something more correct from MVC point of view to pass non-model parameter into a view and get it back then with submited data?
But this parameter should not be stored anywhere where other pages can see it somehow.
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.