Input value does not show up in MVC - c#

I am trying to populate textarea using the input value. But it is not working. Any solution to this issue?
#Html.EditorFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css", #Value = "ViewData.Model.ModelName" } })

You can remove quotes from #Value = "ViewData.Model.ModelName", because you are sending a string to Value property, not the content of ModelName property.
But why don't you use the TextArea helper? Like:
#Html.TextAreaFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css" })
It's not common set the value property like you are doing, I don't know if it works, actually. I think this is not a good way to do that. You should use the TextAreaFor(x => x.Property) or EditFor(x => x.Property) directly, instead of set the value property, as I said.

you don't need to manually populate the value property with the ViewData, remove the #Value = "ViewData.Model.ModelName". I think the problem is your ViewData name conflict with the property of the model (both have the name "ModelName").
Try changing the ViewData property name to something else. Works for me when I have an issue with unpopulated DropDownListFor

Related

How do I make the date field #Html.EditorFor receive date coming from a ViewBag

How do I make the date field #Html.EditorFor receive date coming from a ViewBag.
Controller
var searchDateBD = db.Sequence.ToList().Select(l => l.DateFlow).Max();
ViewBag.date = searchDateBD;
View
#Html.EditorFor(model => model.Sequence.DateFlow,
ViewBag.date,
new { htmlAttributes = new { #class = "form-control input-lg" } })
In this way above developed by me is not correct.
How to do so that the field receives the date coming from the viewbag of the controller
You don't need ViewBag here. You can set the model property in the controller action and pass the model object to view back in the line returning View like:
model.Sequence.DateFlow = db.Sequence.Select(l => l.DateFlow).Max();
return View(model);
You also do not need to materialize the result by calling ToList() and leave the Max item to be calculates on the database server.
and in your view it would be like:
#Html.EditorFor(model => model.Sequence.DateFlow,
new { htmlAttributes = new { #class = "form-control input-lg" } })
You can overwrite the value using attribute #Value:
htmlAttributes = new { #class = "form-control input-lg", #Value = ViewBag.date }
Note that #Value is written with an uppercase letter V.

Excluding time from TextBoxFor when ViewModel setup as DataType of Date

I am trying to properly display a (nullable) Date-type-only field. For this field, my Model and ViewModel classes are defined as....
[DisplayFormat(DataFormatString = "{0:M/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime? PeriodEnd { get; set; }
In my details view (based on model), it's showing the date properly (excluding the time element):
#Html.DisplayNameFor(model => model.PeriodEnd)
Problem: In my edit view (based on ViewModel), it's showing the time also, which I'm trying to exclude. Here's how it's defined.
#Html.TextBoxFor(model => model.PeriodEnd, new { #class = "datepicker" })
I also tried....
#Html.TextBoxFor(model => model.PeriodEnd.Value().ToShortDateString(),
new { #class = "datepicker" })
... but that produced an error.
Is there a better way to accomplish this?
Well, first, you're not utilizing the data annotation. Something like DataType.Date doesn't do anything on its own. Using editor templates (Html.EditorFor), it can be utilized to provide an date type input, but if you use Html.TextBoxFor, it's basically ignored.
You can fix that by either 1) using Html.EditorFor instead or 2) explicitly setting the type: #Html.TextBoxFor(m => m.PeriodEnd, new { type = "date", #class = "datepicker" }).
This was the solution:
#Html.TextBoxFor(m => m.PeriodEnd, "{0:M/d/yyyy}", new { #class = "datepicker" })
Thanks to all for your feedback!

Displaying the Value of a ViewBag in the EditorFor() method of a View using ASP.NET MVC

It is possibly a mistake in my syntax, but I have been unable to set a default value for my EditorFor in my view using ViewBag.
I have checked that the value in the ViewBag.FirstName is being passed through correctly, it is fine. However, the field displays with no value.
My statement is:
#Html.EditorFor(model => model.Person.FirstName, new { htmlAttributes = new { #class = "form-control" } , #Value = ViewBag.FirstName })
Any help would be greatly appreciated.
Apologies for the simplicity of my question.
You should really just do model binding the normal way: by assigning the value of the model property. ViewBag is unmaintainable and not strongly-typed.
If you really do need ViewBag for some reason, just move your assignment to #Value inside your htmlAttributes object, like so:
#Html.EditorFor(model => model.Person.FirstName, new { htmlAttributes = new { #class = "form-control", #Value = ViewBag.FirstName } })
I know this is an old answer and thanks to johnnyRose for the correct one. I simply wanted to add that I was still not able to get the #value passed with it and it was because of the #Value a simple matter of case v=V.. Wanted to share my ignorance in hopes of helping another. :)

ASP.NET MVC 3: Override "name" attribute with TextBoxFor

Is it possible when using Html.TextBoxFor to override the name attribute?
I have tried with no success. I need to use TextBoxFor to get client side validation to work, however for reasons I won't go into I need the name of the textbox to be different from the generated one.
I have tried the following:
#Html.TextBoxFor(x => x.Data, new { name = Model.Key + "_Data", id = Model.Key + "_Data" })
Which works for ID but not name. Is this possible?
Update: Looking into the code for TextBoxFor. It doesn't look like there is an easy way. Hopefully someone can prove me wrong.
Rob, actually there is a much simpler way. Instead of name, use Name:
#Html.TextBoxFor(x => x.Data, new { Name = Model.Key + "_Data", id = Model.Key + "_Data" })
Are you asking this because you want to apply a prefix to the name? If so, you can do this by setting ViewData.TemplateInfo.HtmlFieldPrefix in your Controller.
I learnt a lot about this stuff from Brad Wilson's blog.
EditorFor has an overload where you can supply the name attribute as a parameter:
#Html.EditorFor(expression, null, name)
Try EditorFor. you can pass string as template name if you want to make sure textbox is rendered even if property type is not string. If property is string already, it does not need templatename explicitly to render textbox, so you can pass null. Note that it does not require id parameter explicitly, it will infer it from element name. And all the validation things are still active with EditorFor
#Html.EditorFor(x => x.Data, "string", Model.Key + "_Data")
It is called Microsoft GOTCHA...
Use the name in caps, like this
#Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })
ben's answer got me what I was looking for except you need to wrap in in Html.Raw
#Html.Raw(Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData"))
a little bit "unpretty"=), try:
#Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
For me, it works! I hope that help!
#Html.EditorFor(model => model.Nome, new { htmlAttributes = new { #class = "form-control", #maxlength = "80", #id = "NomeFilter", #Name = "NomeFilter" } })
#Html.EditorFor(Model => Model.Something, "name", "name", new {#class = "form-control" })
Not sure which of those two string parameters in the middle do the work, but it worked only when I typed both of them.
For this example, I was disabling form fields based on permissions, but still showing them. I had a hidden field to send the value to the controller, but wanted a different field name in the EditorFor.
First param after model value represents the "name" property, second is the new name.
#Html.EditorFor(m => m.UserName, "name", "UserNameDisabled", new { htmlAttributes = new { #class = "form-control", #disabled = "disabled"} });
Results in:
<input class="form-control text-box single-line" disabled="disabled" id="UserNameDisabled" name="UserNameDisabled" type="text" value="someEnteredValue" />
Keep it simple, your already providing the ID you should simply be able to use the method "TextBox" instead of "TextBoxFor" and it will work fine client side and server side. In addition, although the accepted answer will work but will produce duplicate Name attributes on your tag if you inspect it using a browser. The below solution does not have that problem.
MvcHtmlString Html.TextBox(string name, string value, object htmlAttributes)
#Html.TextBox(Model.Key + "_Data", Model.Key, new { id = Model.Key + "_Data" }

Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

I am kind of stumped because, I want to format the value and add a html attribute for css class.
If I use #Html.TextBoxFor(m => m.DateModified)
- I can add html attribute but formatting does not work via DisplayFormat attribute on the member.
If I use #Html.EditorFor(m => m.DateModified)
- Formatting works but I cannot add html attribute
If I use #Html.TextBox("DateModified", Model.DateModified, ...)
- I get null reference exception when Model is null when the form is in add mode
What is the best way to achieve this?
I ended up solving this by creating a custom editor template for my date picker as so:
Shared/EditorTemplates/DateTime.cshtml
#model System.DateTime?
#Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty, new { #class = "date-picker" })
Then in my original page continue to use
#Html.EditorFor(m => m.DateModified)
You could...
#Html.TextBoxFor(m => m.DateModified, new { Value = Model.DateModified.ToString("MM-dd-yyyy"), #class = "superCoolClassName"})
Use #Html.EditorFor(m => m.DateModified), because otherwise the DisplayFormat attribute will have no effect.
To add further attributes like a CSS class, you have to create an editor template for the DateTime.
Create a file EditorTemplates/DateTime.cshtml with the following content:
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
{
#class="date"
})
Please note that the value of the TextBox is not set with the Model directly, but rather with the TemplateInfo.FormattedModelValue, because that value will be formatted according to the DisplayFormat attribute while the Model not. (This took me quite some time to realize. :))
In simple cases this might be enough, e.g. if the CSS class can be the same for all date editors.
If you want to parametrize the attribute, you can do that as well, passing the attribute value parameter to the EditorFor.
#Html.EditorFor(m => m.DateModified, new { #class = "someClass" })
However, this parameter will be not automagically delegated to the HTML control as attribute, but you have to "handle it" in the template explicitly. According to my experiences you can access this parameter value in the ViewData in the template, so the parametrized template looks like this:
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
{
#class=ViewData["class"]
})
To prevent hardcoding the key/value pairs listed in EditorFor , convert the ViewData object to a Dictionary and pass that dictionary object to TextBox.
eg
#Html.EditorFor(m => m.DateModified, "Template", new { #class = "someClass", size=8 , htmlTag="custom" })
And in the template you have
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData.ToDictionary(c=>c.Key,c=>.Value))
To show json date in textbox (cshtml):
var d1 = ui.item.IssueDate;
var d = new Date(parseInt(d1.slice(6, -2)));
var Issdate = ("0" + (d.getMonth() + 1)).slice(-2) + '/' +
("0" + d.getDate()).slice(-2) + '/' +
d.getFullYear().toString();
$('#IssueDate').val(Issdate);

Categories

Resources