I have :
#Html.EditorFor(model => model.name)
This display my user name but I want to override this name. So I tried this :
#Html.EditorFor(model => model.Password, new { value = "othertext"} )
But this doesn't work, I always have my user name.
Thanks for your help !
The "additional view data" parameter on EditorFor sets up new view data to be used by your editor template(s). It does not make "extra objects" to be rendered as you seem to be using it here.
And as I understand you trying to set htmlAttributes:
#Html.TextBoxFor(model => model.Password, new { value = "xxx" })
Related
My SQL database has a column Date which allows strings only in such format: __/____, basically month/year for example: 01/2019.
Is it possible for my:
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
to have some helper so that user can type only using the correct format? For example the editor which has strong typed something like this: __/___ and can only input numbers to fill underscores.
#Html.TextBoxFor(m => m.StartDate,
new { #Value = Model.StartDate.ToString("yyyy/MM/dd"), #class="datepicker" })
I have a form with a MultiSelectList and I'm using .Net validation to require a selection be made amongst required other vields. When I submit, the multiple selections are sent to the controller correctly but if requirements aren't met, ModelState is invalid, I return back to the form. I'm using the below line to set the ViewBag instance of the MultiSelectList with the selections that were made. The code seems to work. However, the view only displays a single selection, the first selection, not any of the others that were sent and returned.
ViewBag.SelectedServiceLines = new MultiSelectList(db.LookUps.Where(lu => lu.RecordType == "ServLine"), "ID", "Description", funder.SelectedServiceLines.Select(i => i));
If I step through the razor code in my view I can view the ViewData and see that there is a MultiSelectList and it does have all of the selections that were sent to the controller and then returned. But only one of those selections are displayed. Below is how I'm displaying the list. I can't figure out why only one item is being displayed and not the multiple that get sent back and are in the ViewBag as "SelectedItems" for the MultiSelectList.
#Html.DropDownList("SelectedBusinessLines", null, htmlAttributes: new { #class = "form-control chosen-select", #multiple = "multiple" })
You can use either MultiSelectList or IEnumerable<SelectListItem>, depending on your choice:
// MultiSelectList approach
ViewBag.SelectedServiceLines = new MultiSelectList(db.LookUps.Where(lu => lu.RecordType == "ServLine"), "ID", "Description", funder.SelectedServiceLines.ToList());
// IEnumerable<SelectListItem> approach
ViewBag.SelectedServiceLines = db.LookUps.Where(lu => lu.RecordType == "ServLine")
.Select(i => new SelectListItem() {
Text = i.Description,
Value = i.Id,
Selected = funder.SelectedServiceLines.Contains(i.Id)
}).ToList();
Then use ListBox[For] helper instead of DropDownList[For] to create <select> element with multiple attribute:
#* MultiSelectList approach *#
#Html.ListBoxFor(model => model.SelectedBusinessLines, ViewBag.SelectedServiceLines as MultiSelectList, new { #class = "form-control chosen-select", #multiple = "multiple" })
#* IEnumerable<SelectListItem> approach *#
#Html.ListBoxFor(model => model.SelectedBusinessLines, ViewBag.SelectedServiceLines as IEnumerable<SelectListItem>, new { #class = "form-control chosen-select", #multiple = "multiple" })
Note that the ViewBag properties have dynamic type, you need to cast into respective type to avoid view rendering problem.
Side note
You may try create a MultiSelectList or IEnumerable<SelectListItem> property inside viewmodel:
public MultiSelectList SelectedServiceLines { get; set; }
And populate it with same way as ViewBag mentioned above:
var model = new ViewModel(); // use your viewmodel class name
model.SelectedServiceLines = new MultiSelectList(db.LookUps.Where(lu => lu.RecordType == "ServLine"), "ID", "Description", funder.SelectedServiceLines.ToList());
return View(model);
Then use it with ListBoxFor helper, but this time without any cast unlike ViewBag does:
#Html.ListBoxFor(model => model.SelectedBusinessLines, Model.SelectedServiceLines, new { #class = "form-control chosen-select", #multiple = "multiple" })
Can you try this instead? I've always used a List<SelectListItem> instead of a MultiSelectList:
// Use List<SelectListItem> instead
ViewBag.SelectedServiceLines =
db.LookUps
.Where(lu => lu.RecordType == "ServLine")
.Select(i => new SelectListItem() { Text = i.Description, Value = i.Id })
.ToList();
I also suggest using a ListBox instead:
#Html.ListBox("SelectedBusinessLines", ViewBag.SelectedBusinessLines, htmlAttributes: new { #class = "form-control chosen-select", #multiple = "multiple" })
I have #Html.DropDownList which take data from controller in edit mode. I need to hide DropDown element and show some message if list is null or empty.
I try with this code in view, but all the time give me result thet have, and show empty dropdown:
#if(ViewBag.DatumRID != null)
{
<div class="col-md-10">
#Html.DropDownList("DatumRID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DatumRID, "", new { #class = "text-danger" })
</div>
}
else
{
<h6 style="color:#ff0000"> NO RECORDS.</h6>
}
And code from controller is here:
ViewBag.DatumRID = new SelectList(db.tbl_relacii.Where(x => x.DatumR == tbl_rezervacii.DatumP).OrderBy(x => x.DatumR), "relID", "DatumForDisplay", tbl_rezervacii.DatumRID);
when record fount dropdown is ok, but when record is null dropdown show empty.
Check list size also. For correct displaying data SelectList must contain more than zero items. Try this:
#if(ViewBag.DatumRID != null && ViewBag.DatumRID.Count > 0)
{
<div class="col-md-10">
#Html.DropDownList("DatumRID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DatumRID, "", new { #class = "text-danger" })
</div>
}
else
{
<h6 style="color:#ff0000"> NO RECORDS.</h6>
}
Update:
U may try update your controller code like this:
List<SelectListItem> viewList = new List<SelectListItem>();
viewList.AddRange(new SelectList(db.tbl_relacii.Where(x => x.DatumR == tbl_rezervacii.DatumP).OrderBy(x => x.DatumR), "relID", "DatumForDisplay", tbl_rezervacii.DatumRID));
ViewBag.DatumRID = viewList;
And pass "viewList" object to DropDownList helper at razor markup.
Ok. I searched around, and could find anything reasonable to work with. Selectlists don't have a Count() or any() method like most lists. Here's what I came up with:
#{
var counter=0;
foreach(var a in Viewbag.DatumRID)
{
counter++;
break; //stop it so it doesn't iterate through the whole thing since that's unnecessary
}
}
#if(counter>0){//put your dropdown here}
I am using the code below to make the text entry read only but this does not become read only any idea why this could be
#Html.EditorFor(model => model.UserName, new { Readonly = "Readonly"})
Why cont you use
#Html.EditorFor(model => model.UserName, new { #readonly = true});
or you can use like
#Html.EditorFor(model => model.UserName, new { #readonly = "readonly"});
try textboxfor
#Html.TextBoxFor(model => model.UserName, new { #readonly = true})
The HtmlHelper EditorFor do not have overloads that take HTML attributes.
Instead of EditorFor go with TextBoxFor
#Html.TextBoxFor(model => model.UserName, new { disabled = "disabled", #readonly = "readonly"})
hope it helps
In a view where my user can create a new item, he will have to select an item from a dropdownlist like this:
#Html.DropDownListFor(model => model.m_CustomerId, ViewBag.ListCustomers as SelectList, "--- Select Customer ---", new {#class = "CustomerInfo" })
When the selection is made, there are fields in the current view that would name to be updated with the selection made. Those fields are like those:
Item Name: #Html.TextBoxFor(model => model.m_ItemName, new { #disabled = "disabled" }) #Html.ValidationMessageFor(model => model.m_ItemName)<br/>
Item Date: <span style="font-weight: bold">#Html.DisplayFor(model => model.m_ItemDate)</span> #Html.ValidationMessageFor(model => model.m_ItemDate)<br/>
I've read a few things like maybe using JQuery or Java, but I'm honestly cluesless and I don't know these languages. How could I do this? I do not mind trying out languages, scripts or anything else, but I'm rather a newbie in MVC app and this is puzzling me. Thanks a lot!
So what you might want to do is download and install jQuery via NuGet Package Manager. Once you've gotten that installed then we can extend the controls. You could extend the DropDownListFor by adding some attributes.
#Html.DropDownListFor(model => model.m_CustomerId,
ViewBag.ListCustomers as SelectList,
"--- Select Customer ---",
new
{
#class = "CustomerInfo",
onchange = "function() { $('m_ItemName').val($(this).text()); }"
}
)