multiselect Set values as selected in dropdown in MVC - asp.net-mvc

I need a way to set already selected values in a multi select dropdown
in a edit view in a web application (ASP.NET, Mvc 5) :
<div class="form-group">
<div class="col-md-10">
#Html.DropDownList("EntityId", selectList: (IEnumerable<SelectListItem>)ViewBag.EntityId, htmlAttributes: new { #class = "form-control select2", multiple = "multiple" })
</div>
</div>
//Controller
ViewBag.EntityId = new SelectList(context.CompanyEntity.Where(x => x.UserProfile.Any(u => u.UserProfileId == id)), "EntityId", "EntityName");
I need a edit view by set selecting already selected values.

Related

how to store user input value in local variable(in view) in asp.net mvc 5?

i want to declare a local variable in my razor html form (view) and want to store user input value in that local variable.And then want to use that variable in foreach loop .in mvc 5 i am new to asp.net.
#using (Html.BeginForm("AddQuestion", "admin", new { id = ViewBag.qf_id }, FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-group">
<label>Question Statement</label>
#Html.TextBoxFor(a => a.Ques.QuestionString, new { #class = "form-control" })
#Html.ValidationMessageFor(a => a.Ques.QuestionString)
</div>
<div class="form-group">
<label>No. of option you want to add for this question </label>
<div class="col-md-4">
#{var val = #Html.TextBoxFor(a => a.counter, new { #class = "form-control" }); }
</div>
</div>
foreach(var item in val)
{
#Html.TextBoxFor(a=>a.Ans.AnswerStatement)
}
<div>
<button class="btn btn-primary" type="submit">Add</button>
</div>
}
What you are trying to do won't work unless it gets adapted to 1 of 2 solutions:
1) The only way this works is if the use enters a value in the textbox, then posts back to the controller, and the controller inputs this variable into the model, which the view then re-renders, using the number in the model, with the desired number of entries. That's the web forms way and is inefficient.
2) Using JavaScript, when a user enters a number, JavaScript can either render the markup using a templating framework, or make a request to the server, get the individual data, and return a partial view as HTML.

Dropdown lists first option to be null

I Have a dropdownlist that allows you to selcect an exsiting Driver ID. when the page loads the first Driver ID is already selected and I would rather allow the Driver ID dropdownlist to first have a null value selected when the page loads. How will I do this?
This is my Driver ID view:
<div class="form-group">
#Html.LabelFor(model => model.DriverID, "Driver Cell", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("DriverID", null, htmlAttributes: new { #class = "box" })
#Html.ValidationMessageFor(model => model.DriverID)
</div>
</div>
You can use
#Html.DropDownList("DriverID", null, "Select a driver", htmlAttributes: new { #class = "box" })
but this dropdown will be empty since you are passing null as SelectList.
You can also use the asp-for tag helper with a SelectListItem list
Then this is what you write in your form in the view
<select asp-for="(Whatever the value is associated to)" asp-items="The List<SelectListItem> list">
<option disabled selected>---Select---</option>
<!-- This line will go to the top of the dropdown list -->
</select>
This will create a dropdown list where by default nothing is selected and this default value will be null
You can also refer to this other post on stack overflow:
Set Default/Null Value with Select TagHelper

Custom entry on #Html.DropDownListFor

The code below will only accept the colors contained in the model. How can it allow for the user to enter one that is not listed in the model ?
<div class="form-group input-group-sm">
#Html.LabelFor(m => m.CarColor, new { #class = "control-label col-md-5" })
<div class="col-md-7">
#Html.DropDownListFor(m => m.CarColor, ViewBag.CarColorList as SelectList })
</div>
</div>
The way that I would do it would to be to use Select2. Select2 is basically an awesome drop down list. The 'Loading Data' example shows you where to start.

Value of TextBox with Disabled propertie isn't pass to controller

If I put a disabled='disabled' propertie in a TextBox with Razor, when I submit my form, the value of this TextBox isn't sent to the controller.
#using (Html.BeginForm("Principal", "Home")) {
<div class="form-group">
#Html.LabelFor(m => m.Fornecedor, new { #class = "col-sm-2 control-label" })
<div class="col-sm-9">
#Html.TextBox("Fornecedor", Model.Fornecedor, new { #class = "form-control", type = "text", disabled = "disabled" })
</div>
</div>
}
When submitted, the Model.Fornecedor value in controller is null, but without the disabled propertie, the value return correctlly.
Can someone help me, to put a TextBox field disabled but pass his default value to controller when the form is submitted ?

How to filter dropdownlist using another dropdownlist with angularJs

I've got 2 dropdownlist :
<div class="control-group">
#Html.LabelFor(x => x.ServiceId)
#Html.DropDownListFor(x => x.ServiceId, new SelectList(Model.ServiceList, "Id", "Title"), new { #class = "open" })
</div>
<div class="control-group">
#Html.LabelFor(x => x.ServiceShareId)
#Html.DropDownListFor(x => x.ServiceShareId, new SelectList(Model.ServiceShareList, "Id", "Title"), new { #class = "open" })
</div>
I want to filter serviceshare values by service dropdownlist selected value. how to do?
To filter the dropdownlist using angular pls find
the below link for help.
http://plnkr.co/edit/n7TebC?p=preview

Resources