I'm am dealing with a problem in Razor View. DropDown is working fine but the selected value is not setting properly. The value is set from the controller but in the view #Html.DropDownListFor value is not setting. Any help will be highly Appreciated. Thanks in Advance! Here is my Code.
View:
#Html.LabelFor(x => x.BranchID, htmlAttributes: new { #class = "col-2 col-form-label" })
<div class="col-md-3">
#Html.DropDownListFor(x => x.BranchID, new SelectList(Model.listofBranch, "BranchID", "BranchName"), new { #class = "form-control m-input", #id = "BranchID" })
#Html.ValidationMessageFor(model => model.BranchID, "", new { #class = "text-danger" })
</div>
Controller
mdlGRN.BranchID = obj.BranchID;
return View(mdlGRN);
In the view code the selected value is not specified, when you are calling:
#Html.DropDownListFor(x => x.BranchID,
new SelectList(Model.listofBranch, "BranchID", "BranchName"),
new { #class = "form-control m-input", #id = "BranchID" })
There is no value passed to the SelectList Constructor to indicate the SelectedValue
It should be something like this asuming that the value you want selected is the one saved into the model:
#Html.DropDownListFor(x => x.BranchID,
new SelectList(Model.listofBranch, "BranchID", "BranchName", Model.BranchID),
new { #class = "form-control m-input", #id = "BranchID" })
Related
I render a PartialView inside my main View
View:
#model FullProductViewModel
...
<div class="send-container">
#Html.Partial("_CommentForm", new CommentViewModel { ProductId = Model.Id, Title = "" , Context="" })
</div>
...
I have a filed with name "Title" in Both my classes "FullProductViewModel" and "CommentViewModel".
_CommentForm PartialView:
#model CommentViewModel
<p class="page-title">#Resources.SendComment</p>
#using (Html.BeginForm("Comment", "Home", FormMethod.Post, new { #class = "form-comment form-submit" }))
{
#Html.AntiForgeryToken()
<div class="input-group">
#Html.LabelFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title, "", new { #class = "text-danger" })
#Html.EditorFor(model => model.Title, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="input-group">
#Html.LabelFor(model => model.Context)
#Html.ValidationMessageFor(model => model.Context, "", new { #class = "text-danger" })
#Html.TextAreaFor(model => model.Context, htmlAttributes: new { #class = "form-control", rows = "8" })
</div>
<div class="input-group">
#Html.LabelFor(model => model.CaptchaCode)
#Html.ValidationMessageFor(model => model.CaptchaCode, "", new { #class = "text-danger" })
#Html.EditorFor(model => model.CaptchaCode, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="captcha-wrapper">
<img alt="Captcha" id="imgcaptcha" src="#Url.Action("Captcha", "Captcha")" />
</div>
<button class="btn btn-submit" type="submit">#Resources.SendMessage</button>
}
The problem is "Title" inside PartialView take its value form "FullProductViewModel" model but not "CommentViewModel ". i can easily change the field name "Title" inside one of models to solve problem... but why this happened and how i can fix it without change field name?
Your Title property is conflicting with the #{ ViewBag.Title = ".. line of code in your view (the ViewBag code is used by the _Layout.cshtml file to set the global <title> attribute for the page).
All the HtmlHelper methods that generate form controls set the value by checking (in order)
ModelState
The ViewDataDictionary (ViewData or ViewBag)
The value of the models property
In your case, nothing has been added to ModelState. But because a value for Title has been added to the ViewDataDictionary, its that value which is used for the value of your textbox. The method never gets to read the actual value you have set in your model.
The only realistic option (unless you want to modify the code in the _Layout and all views that use it) is to change the name of the property.
<div class="form-group">
<label>Quoted With New Combo Endorsement</label>
#Html.DropDownListFor(a => a.Quoted_With_New_Combo_Endorsement, new[]
{
new SelectListItem {Text = "Yes", Value = "Yes"},
new SelectListItem {Text = "No", Value = "No"},
}, "Select the Value", new { style = "width:250px", #class = "form-control" })
</div>
How to convert this code to the checkbox for(Yes/NO)?
Reminder: datatype can't be changed to bool
I've done this in a project and it's working fine.
<div class="form-group">
<label class="col-md-2 control-label">Scan in Colour?</label>
<div class="col-md-10">
<div class="radio-group">
#Html.RadioButtonFor(m => m.ScanInColour, "Yes",
new { #id = "ScanInColourYes", #class = "form-control" })
<span>Yes</span>
#Html.RadioButtonFor(m => m.ScanInColour, "No",
new { #id = "ScanInColourNo", #class = "form-control" })
<span>No</span>
</div>
</div>
</div>
Shouldn't it be a radio-button because it either yes or no?
Instead of the #Html.DropDownListFor use:
#Html.RadioButton("YesNo","Yes")
#Html.RadioButton("YesNo","No")
I want to get selected value in dropdownlist.
My View:
<div class="form-group">
#Html.LabelFor(model => model.AccountRefNo, "Account Desc", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("AccountRefNo", ViewBag.AccountRefNo as SelectList, "Account Desc", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.AccountRefNo)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PartyID, "Party Type", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("TypeID", ViewBag.TypeID as SelectList, "Party Type", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.TypeID)
</div>
</div>
And, my Controller:
ViewBag.TypeID = new SelectList(db.PartyTypes, "TypeID", "TypeDesc", party.TypeID);
string[] codes = { "2.20.40.10", "3.50.10.20" };
ViewBag.AccountRefNo = new SelectList(db.ChartOfAccounts.Where(a => codes.Contains (a.AccountCode)), "AccountCode", "AccountDesc", party.AccountRefNo);
return View(party);
It seems it is OK but I don't know where the problem is. Both dropdownlists filling the lists but both are selecting their OptionLabel i.e selecting "Party Type" and "Account Desc" respectively.
Please help. Thanks!
This is in Edit controller and following worked.
string typeDescSelected = (from sub in db.PartyTypes
where sub.TypeID == party.TypeID
select sub.TypeDesc).First();
ViewBag.TypeDesc = new SelectList(db.PartyTypes, "TypeID", "TypeDesc", typeDescSelected);
string[] codes = { "2.20.40.10", "3.50.10.20" };
string accountDescSelected = (from sub in db.ChartOfAccounts
where sub.AccountCode == party.AccountRefNo
select sub.AccountDesc).First();
ViewBag.AccountDesc = new SelectList(db.ChartOfAccounts, "AccountCode", "AccountDesc", accountDescSelected);
return View(party);
SelectList constructors' first parameter needs to take a collection of SelectListItem:
Try the following and it should work provided there is data (void of null refs on PartyTypes and ChartOfAccounts:
ViewBag.TypeID = new SelectList
(
db.PartyTypes.Select(m=>new SelectListItem {Text=m.TypeDesc, Value=m.TypeID})
, "TypeID"
, "TypeDesc"
, party.TypeID
);
ViewBag.AccountRefNo = new SelectList
(
db.ChartOfAccounts
.Where(a => codes.Contains (a.AccountCode))
.Select(m=>new SelectListItem({Text=m.AccountDesc, Value=m.AccountCode}))
, "AccountCode"
, "AccountDesc"
, party.AccountRefNo //this maybe wrong
);
Also if ViewBag.AccountRefNo and party.AccountRefNo are the same type, then the last argument for the second SelectList constructor has to be incorrect. The argument should be the selected value so most likely party.AccountCode?
How can I give my TextBox, ng-click attribute when using Html.TextBoxFor?
#Html.TextBoxFor(model => model.User.Firstname,
new {
#class = "form-control required",
placeholder = "نام"
})
Add ng_click to htmlAttributes object.
#Html.TextBoxFor(model => model.User.Firstname, new
{
#class = "form-control required",
placeholder = "نام" ,
ng_click = "yourFunc()"
})
*Note : The _ instead of - in ng_click is because C# does not accept -
I have the following inside my asp.net mvc web application :-
<div><span class="f">Data Center Name</span> #Html.EditorFor(model => model.Switch.TMSRack.DataCenter.Name, new { disabled = "disabled" })</div>
but the field will not be disabled ,, can anyone adivce please?
THanks
#Html.EditorFor() does not have an overload to support htmlAttributes. You could try #Html.TextBoxFor()
#Html.TextBoxFor(model => model.propertyName, new {disabled= "disabled" })
If you are using system key words such as class in htmlAttributes please add # before the attribute name.
Ex:
#Html.TextBoxFor(model => model.propertyName, new {#class = "disabledClass" })
Using MVC 5, #Html.EditorFor() supports htmlAttributes
#Html.EditorFor(model => model.x, new { htmlAttributes = new { #class = "form-control", #disabled = "disabled" } })
The above example shows how you can add a class and also the disabled attribute
Another alternative: surround the EditorFor with a div or a span with a certain id, and then use a bit of jquery/js:
<span id="editors">
#Html.EditorFor(x => x.ViewModelProperty)
</span>
<!-- Disable above editors. EditorFor doesn't allow html attributes unfortunately. -->
<script type="text/javascript">
$(function () { $('#editors :input').attr("disabled", true); });
</script>
For those that lose your text value : Disabled fields does not pass its value to the controller. Instead, use #readonly = "readonly"
#Html.EditorFor(model => model.EmployeeCode, new { htmlAttributes = new { #readonly = "readonly", #class = "form-control"} })
If you lose information when you accept the Edit-changes ...
You could try
<h3>#Model.x</h3>
#Html.HiddenFor(model => model.x, new { htmlAttributes = new { #class = "form-control" } })
You can also use EditorFor()
eg:
#Html.EditorFor(model => model.Nama, new { htmlAttributes = new { #disabled ="true", #class = "form-control" } })