getting null value in textboxfor tool for date in mvc - asp.net-mvc

I am adding bootstrap date in textboxfor tool and its showing also.
But, the problem is that I am getting null value in controller.
Here is my code
View
#using (Html.BeginForm("Create", "StaffRegistration", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
#Html.TextBoxFor(model => model.JoinDate, new { #class = "form-control", placeholder = "DD/MM/YYYY", Name = "date" })
}
Script File
$(document).ready(function () {
var date_input = $('input[name="date"]'); //our date input has the name "date"
var container = $('.bootstrap-iso form').length > 0 ? $('.bootstrap-iso form').parent() : "body";
date_input.datepicker({
format: 'dd/mm/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
})
})
Controller
public ActionResult Create(StaffRegistrationViewModel staffRegistrationViewModel)
{
//here is my code
}
Now, when I click the button at that time at controller side null value is coming.[I saw by doing in debug mode]
In staffRegistrationViewModel, JoinDate field showing null value.
Thank You.

Generally Bootstrap DateTime Picker will Implement like this...
and in this way even you will get the value of selected datetime in post action method(in controller)...
// View Code
<div id="datefrom" class="input-group date">
#Html.TextBoxFor(model => model.FromDate, new { #class = "form-control", #readonly = "readonly", style = "position: static;" })
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
// Jquery Code
$('#datefrom').datetimepicker({
format: 'DD/MM/YYYY HH:mm:ss',
ignoreReadonly: true,
useCurrent: false});

Related

ASP.NET MVC - How to Specify the Value in TextBox

How do I specify the Value that should be entered into the Textbox. I dont want to use dropdownlist. I want to accept only these three (3) values Nigeria, Ghana and Togo
If any other value is entered it should not save or hide or disable the Save button.
View
<div class="col-md-12">
<div>
#Html.LabelFor(model => model.COUNTRY_NAME, "Country Name")
#Html.TextBoxFor(model => model.COUNTRY_NAME, new { #style = "border-radius:3px;", #type = "text", #class = "form-control", #placeholder = "Country Name", #autocomplete = "on" })
#Html.ValidationMessageFor(model => model.COUNTRY_NAME, null, new { #class = "text-danger" })
</div>
</div>
Please how do I achieve this?
There are two Options
You can use Server side Custom Validator for Country_Name property.
Or Jquery validation on client side.
When you submit the form, you can validate textbox value by jQuery.
<button onclick="validateData();"></button>
Then write a validation method as follows.
function validateData() {
if (Nigeria, Ghana and Togo) {
//ajax post to your controller
} else {
//return error message
}
}

Multiple parameters in mvc4

I am learning MVC and am stuck at this. I was trying to create a filter for name, category, type, Min Value, Max value but when redirected to another view it only shows three parameters but without other values.
Controller (DetailController)
public ActionResult FilteredResult(string searchedLocation, string Type, string PCName, string MinValue, string MaxValue)
{
var A = searchedLocation;
var B = Type;
var C = PCName;
var D = MinValue;
var E = MaxValue;
return View();
}
View
#using (Html.BeginForm("FilteredResult", "Detail", FormMethod.Get))
{
<form>
<h4 style="color: #ed145b;">Find the right property for you</h4>
<ul class="form-style-1">
<li>
#Html.TextBox("Location", ViewBag.Location as string , new { #class = "field-long" })
</li>
<li>
#Html.DropDownList("Type", (SelectList)ViewBag.TypeList, "Select" , new { #class = "field-select" })
</li>
<li>
#Html.DropDownList("Category", (SelectList)ViewBag.CategoryList, "Select" ,new { #class = "field-select" })
</li>
<li>
<label for="amount">Price range:</label>
<input type="text" id="amount" class="field-long" readonly style="border:0; color:#ee2265; font-weight:bold;">
</li>
<li class="clearfix"> #Html.TextBox("MinValue", ViewBag.MinValue as string, new { #class = "Minprice field-long", disabled = "true" })
#Html.TextBox("MaxValue", ViewBag.MaxValue as string, new { #class = "Maxprice field-long", disabled = "true" })
</li>
<li>
<div id="slider-range"></div>
</li>
<li>
<input type="submit" value="Search" class="btn-primary" />
</li>
</ul>
</form>
}
After clicking on search it shows like this. Only the data from dropdown is shown and other parameter like Minvalue and max value is not shown in the url. Location is showing
Can anyone help me on how I can successfully get data in the required field in the controller?
It's because your MinValue and MaxValue textboxes have disabled="true" attribute. Disabled inputs won't be submitted
#Html.TextBox("MinValue", ViewBag.MinValue as string, new { #class = "Minprice field-long", disabled = "true" })
#Html.TextBox("MaxValue", ViewBag.MaxValue as string, new { #class = "Maxprice field-long", disabled = "true" })
You should remove the disabled attribute
#Html.TextBox("MinValue", ViewBag.MinValue as string, new { #class = "Minprice field-long" })
#Html.TextBox("MaxValue", ViewBag.MaxValue as string, new { #class = "Maxprice field-long" })
If you want to make those textboxes non editable, use readonly attribute instead
#Html.TextBox("MinValue", ViewBag.MinValue as string, new { #class = "Minprice field-long", #readonly = "readonly" })
#Html.TextBox("MaxValue", ViewBag.MaxValue as string, new { #class = "Maxprice field-long", #readonly = "readonly" })
because you are using disabled attribute true that means it fetches data from database but will not show on View so either make disabled = "false" or remove this attribute.

Cascaded dropdown Validation not working with Data Annotated as Required

ISSUE / Error EXPLANATION :
I have a cascaded dropdownlist in which first user will select a
country and as per his selection cities are getting populated via ajax in which no city is selected initially but just a SELECT option is selected. I have populated my cities at the change event of country dropdown using ajax JSON data from the controller but it seems to me when I call #CityId.Empty() its removing all options from the city including the SELECT option which I re inserted through jquery code but that line is getting treated as an Option Item instead of OPTION SELECT due to which validation is assuming it a user selection where as in reality its a message to the user to select any given city. SO please help me out to do that validation working if no city is selected it should not allow me to submit the form.
Controller CODE :
public ActionResult PostAd(ClassifiedItem classifiedItem)
{
if (ModelState.IsValid)
{
classifiedItem.PostedDate = DateTime.Now;
classifiedItem.Obsolete = false;
classifiedItem.Status = "POSTED";
db.ClassifiedItems.Add(classifiedItem);
db.SaveChanges();
return View("ImageUploader", classifiedItem);
}
else
{
if (classifiedItem.City_Id == 0 || classifiedItem.City_Id == null)
{
ViewBag.Country_Id = new SelectList(db.Countries, "Id", "Country_Name");
ViewBag.City_Id = new SelectList(new List<City>(), "Id", "City_Name",0);
}
else
{
SetCascadedCountry(classifiedItem.City_Id);
}
Prepare_LOV_Data();
}
return View(classifiedItem);
}
View Code :
<div class="form-group">
<label class="control-label col-md-3">Country</label>
<div class="col-md-6">
#Html.DropDownList("Country_Id", null, "--Select Country--", htmlAttributes: new { #class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">City</label>
<div class="col-md-6">
#Html.DropDownList("City_Id", null,"--Select City--", htmlAttributes: new { #class = "form-control"})
#Html.ValidationMessageFor(model => model.City_Id, "", new { #class = "text-danger" })
</div>
</div>
My Script
$('document').ready(function () {
$('#Country_Id').on('change', function (d) {
$.ajax({
url: 'GetCities',
method: 'Get',
cache: false,
data: { CountryId: $(this).val() },
success: function (d) {
$('#City_Id').empty();
//$('#City_Id').append($('<option/>', { value: 0, text: '--Select City--' }));
$(d).each(function (index, item) {
$('#City_Id').append($('<option/>', { value: item.Id, text: item.City_Name }));
})
},
error: function (d) {
alert(d);
}
})
});
})

Setting default date for datepicker in mvc

I have implemented moment js for my datepicker in a MVC website but I cant get the default date to work. I just shows 1. january 2015. I want it to show todays date.
Model
public class TeamModel
{
public Team Team { get; set; }
}
Controller. I set my start and end date:
model.Team = new Team();
model.Team.StartDate = DateTime.Today;
model.Team.EndDate = DateTime.Now.AddDays(30);
In my View I have the js:
<script type="text/javascript">
$(function () {
moment.lang('da');
$('#Team_StartDate').datetimepicker({
locale: 'da',
format: 'D-MMMM-YYYY',
showClose: true,
showClear: true,
toolbarPlacement: 'top'
});
$('#Team_EndDate').datetimepicker({
locale: 'da',
format: 'D-MMMM-YYYY',
showClose: true,
showClear: true,
toolbarPlacement: 'top'
});
});
</script>
and the editorfor
<div class="form-group">
#Html.LabelFor(model => model.Team.StartDate, htmlAttributes:
new { #class = "control-label col-md-2" })
<div>
#Html.EditorFor(model => model.Team.StartDate, new {
#Value = Model.Team.StartDate.ToString(), htmlAttributes =
new { #class = "form-control" } })
#Html.ValidationMessageFor(model =>
model.Team.StartDate, "", new { #class = "text-danger" })
</div>
</div>
But the date displayed in StartDate is "1-januar-2015" and "31-januar-2015" in EndDate.

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 ?

Resources