Access public virtual dropdown value - asp.net-mvc

I have a dropdown list and a datefield. If there is one specific value selected from the dropdown, the datefield should be required.
To solve that, I tried to use the requiredIf sttribute from the FoolProof library.
But I don't know how to access the selected value in the Model.
Model
//This is the datefield
[RequiredIf(tabState == "discarded")]
[DataType(DataType.Date)]
[Display(Name = "date discarded")]
public Nullable<System.DateTime> datDiscarded { get; set; }
//This is the dropdown
public virtual tabState tabState { get; set; }
tabState in Controller
ViewBag.intIdeaStateID = new SelectList(db.tabState, "intStateID", "strState");
dropdown in View
<div class="form-group">
#Html.LabelFor(model => model.intIdeaStateID, "State", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("intIdeaStateID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.intIdeaStateID, "", new { #class = "text-danger" })
</div>
</div>
How can I check if the selected value from the dropdown is "discarded"?
Thanks for your help.

Related

Value from DropDownListFor is not saving value to Database

Here is my view
<div class="form-group">
#Html.LabelFor(model => model.SubCatagory, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.SubCatagory, new SelectList(ViewBag.SubCategories, "Id", "Name", "Category"))
#Html.ValidationMessageFor(model => model.SubCatagory, "", new { #class = "text-danger" })
</div>
</div>
And here is the method where I'm saving data.
dbContext.Products.Add(product);
dbContext.SaveChanges();
It is saving all the values to database except SubCatagoryId..
Can anyone please help me ?
The SubCatagory property contains null value because it's a complex object property bound for SubCatagory class, which DropDownListFor returns single selected value instead of entire class properties. You should do one of these:
Option A: Create selected value property
Create an int property which will hold selected value from DropDownListFor on Product class, and then you can assign SubCatagory property inside controller by querying from database based from passed value in newly created property.
Model
public class Product
{
[Key]
public int Id { get; set; }
[Display(Name = "Sub Catagory")]
public int SelectedSubCatagory { get; set; }
// other properties
}
View
<div class="form-group">
#Html.LabelFor(model => model.SelectedSubCatagory, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.SelectedSubCatagory, new SelectList(ViewBag.SubCategories, "Id", "Name", "Category"))
#Html.ValidationMessageFor(model => model.SelectedSubCatagory, "", new { #class = "text-danger" })
</div>
</div>
Option B: Use existing child property of SubCatagory class
Suppose you have ID property declared inside SubCatagory class like this:
public class SubCatagory
{
public int Id { get; set; }
public string Name { get; set; }
// other properties
}
You may use it to bind on DropDownListFor helper as in example below:
View
<div class="form-group">
#Html.LabelFor(model => model.SubCatagory.Id, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.SubCatagory.Id, new SelectList(ViewBag.SubCategories, "Id", "Name", "Category"))
#Html.ValidationMessageFor(model => model.SubCatagory.Id, "", new { #class = "text-danger" })
</div>
</div>
Note that if you take this option, make sure that SubCatagory property is already instantiated (with new keyword) in Product class to avoid NullReferenceException when using its child properties.
Reference:
How to simple Html.DropDownListFor MVC.NET (Nested Viewmodels)

Recovery datetime value in the edit view

When i click in the edit view the datetime lost the value.
enter image description here
<div class="form-group">
#Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DataNascimento, "", new { #class = "text-danger" })
</div>
</div>
I would like, when i click in the edit view the value of DataNascimento recive the value from the database.
I hope you guys can help me.
You must force the control to display to dd/mm/yyyy formatted date. to do this you need to format the date.decorate the date property in model as below
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime DataNascimento{ get; set; }
If you are using MVC5,this should work
#Html.EditorFor(model => model.DataNascimento, new {htmlAttributes = new {#Value = #Model.DataNascimento.ToString("dd-MM-yyyy") } })

How to realize DropDownListFor in View?

I want to show all names of "Category" in the dropdownlist in Create View by HtmlHelper.
The Model of "Category" is:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
The Controller is:
public ActionResult Create()//
{
ViewBag.Categorys = new SelectList(categoryRepo.SelectAll(), "Id", "Name");
return View();
}
The View is:
<div class="form-group">
#Html.LabelFor(model => model.CategoryId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.CategoryId, (SelectList)ViewBag.Categorys, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CategoryId, "", new { #class = "text-danger" })
</div>
</div>
You may try this,
#Html.DropDownList("Categorys", null, "Select Category", new {#class="form-control"})

DataAnnotation and value by default

I have a property in view model:
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime DateTime { get; set; }
and view:
<div class="form-group">
#Html.LabelFor(model => model.DateTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DateTime, "", new { #class = "text-danger" })
</div>
</div>
and page shows default value as 01/01/0001 on form:
I want have not any default value (user should specify correct value and can't send form without it). How to remove default value?
You have to make public System.DateTime DateTime { get; set; } a nullable DateTime.

I can not save date in MVC

I've this model which has a couple of columns and one of them is date, but on post I get nothing of date, only in case if I give it the value of DateTime.Now then it will save the current date but If had selected some other date in the datepicker it will not save that as it should, so How can I pass the date to the view model on post, I can pass all the date from dropdown and textboxes but date is no where to found. Here is the code
My view model
[Display(Name = "Requested Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? RequestDate { get; set; }
[Display(Name = "Job Number")]
public string JobNo { get; set; }
Here is the Post Create method code
if (ModelState.IsValid)
{
var model = new PECEquipmentRequest()
{
ProjectId = pecEquipmentRequest.ProjectId,
JobNo = pecEquipmentRequest.JobNo,
RequestDate = pecEquipmentRequest.RequestDate
};
db.PECEquipmentRequests.Add(model);
db.SaveChanges();
}
and here is the my razor syntax
<div class="form-group">
#Html.LabelFor(model => model.RequestDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RequestDate, new { htmlAttributes = new { #Value = DateTime.Now,#class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.RequestDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.JobNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.JobNo, "", new { #class = "text-danger" })
</div>
</div>
and here is the kendo date picker that I'm using
$(document).ready(function () {
$(".datepicker").kendoDatePicker();
});
You can parse the date to the required format in the server side.
DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

Resources