MVC File uploads collection is always null - asp.net-mvc

This was working and now for reasons unknown my file collection is null
VIEW
#section termimalContent {
#using (Html.BeginForm("Add", "Terminals_Policies", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>
<h2>
Create new policy</h2>
</legend>
<p>
<strong>Assigning devices to Node:</strong> #Model.GroupName</p>
<div class="editor-label">
#Html.LabelFor(model => model.PolicyName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PolicyName)
#Html.ValidationMessageFor(model => model.PolicyName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PolicyType)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.SelectedItem, new SelectList(Model.PolicyType, "Value", "Text"),new {#class = "PackageDDL"})
#Html.ValidationMessageFor(model => model.SelectedItem)
</div>
#foreach (var pick in Model.PackageTypeItems)
{
<div class="editor-label">
<label for="#pick.Name">
#pick.Name:</label>
</div>
<div class="editor-field">
<input class="text-box single-line" type="file" name="#pick.Name.Trim()" id="#pick.Name.Trim()" data-val="#pick.IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
#Html.ValidationMessage(pick.Name.Trim())
</div>
}
#Html.HiddenFor(model => model.GroupId)
#Html.HiddenFor(model => model.GroupName)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
}
HTML Generated
<form action="/Terminals_Policies/Add/1" enctype="multipart/form-data" method="post"> <fieldset>
<legend>
<h2>
Create new policy</h2>
</legend>
<p>
<strong>Assigning devices to Node:</strong> Root</p>
<div class="editor-label">
<label for="PolicyName">PolicyName</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-length="Policy name cannot be longer than 50 characters." data-val-length-max="50" data-val-required="Please enter the policy name" id="PolicyName" name="PolicyName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="PolicyName" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="PolicyType">PolicyType</label>
</div>
<div class="editor-field">
<select class="PackageDDL" data-val="true" data-val-number="The field SelectedItem must be a number." id="SelectedItem" name="SelectedItem"><option value="1">IT application</option>
<option value="3"> definition</option>
<option value="4">definition</option>
<option value="5">project</option>
<option value="6">relay schedules</option>
<option value="7">table data</option>
<option value="9">transfer definition</option>
<option value="10">firmware update request</option>
<option value="11"> firmware update request</option>
<option value="12">dat</option>
<option value="15"> firmware</option>
<option value="16"> hex</option>
<option value="17">project</option>
<option value="18">firmware</option>
</select>
<span class="field-validation-valid" data-valmsg-for="SelectedItem" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="IT application file ">
IT application file :</label>
</div>
<div class="editor-field">
<input class="text-box single-line" type="file" name="IT application file" id="IT application file" data-val="true" data-val-required="Please select a file" />
<span class="field-validation-valid" data-valmsg-for="IT application file" data-valmsg-replace="true"></span>
</div>
<input data-val="true" data-val-number="The field GroupId must be a number." data-val-required="The GroupId field is required." id="GroupId" name="GroupId" type="hidden" value="1" />
<input id="GroupName" name="GroupName" type="hidden" value="Root" />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
Controller:
[HttpGet]
public ActionResult Add(int id)
{
_polAdd = CreatePolicyAddModel(id);
return View(_polAdd);
}
[HttpPost]
public ActionResult Add(IEnumerable<HttpPostedFileBase> files, vmPoliciesAdd model)
{
_policyLogic.AddPolicyFile(files,model.PolicyName,(int)model.SelectedItem, "FILE");
return View();
}
On the post action of controller the colelction is empty, can anyone see an obvious mistake

Naming issue:
<input class="text-box single-line" type="file" name="IT application file" id="IT application file" data-val="true" data-val-required="Please select a file" />
Should of course be:
<input class="text-box single-line" type="file" name="files" id="IT application file" data-val="true" data-val-required="Please select a file" />
So fix your Razor code and make sure you have applied correct name to your file input if you expect the model binder to be able to bind to an action argument called files:
<input class="text-box single-line" type="file" name="files" id="#pick.Name.Trim()" data-val="#pick.IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
Ah an by the way id's cannot contain spaces. So you've got a broken HTML. The following seems wrong: id="#pick.Name.Trim()" as well.

Related

How to load value of last employeeid + 1 in add action

I have controller name emp have action add
i need when action add view loaded display last value increased by one in
employeeid textbox .
meaning suppose i have in employee table employeeid value 1 then when action add
view loaded employeeid must have 2 .
so that how to do that please ?
in action what i write
[httpget]
Hide Copy Code
public IActionResult Create()
{
what i write here
}
on view add
what i write here
<div class="form-group">
<label asp-for="EmployeeId" class="control-label"></label>
<input asp-for="EmployeeId" class="form-control" />
<span asp-validation-for="EmployeeId" class="text-danger"></span>
</div>
What I have tried:
var results = _context.GetAll().Where(Employee => Employee.EmployeeId> Employee.EmployeeId).OrderBy(Employee => Employee.EmployeeId).FirstOrDefault();
and i pass model to view
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmployeeId" class="control-label"></label>
<input asp-for="EmployeeId" class="form-control" />
<span asp-validation-for="EmployeeId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BranchCode" class="control-label"></label>
<input asp-for="BranchCode" class="form-control" />
<span asp-validation-for="BranchCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmployeeName" class="control-label"></label>
<input asp-for="EmployeeName" class="form-control" />
<span asp-validation-for="EmployeeName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmployeeAge" class="control-label"></label>
<input asp-for="EmployeeAge" class="form-control" />
<span asp-validation-for="EmployeeAge" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="JoinDate" class="control-label"></label>
<input asp-for="JoinDate" class="form-control" />
<span asp-validation-for="JoinDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BirthDate" class="control-label"></label>
<input asp-for="BirthDate" class="form-control" />
<span asp-validation-for="BirthDate" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input asp-for="Active" /> #Html.DisplayNameFor(model => model.Active)
</label>
</div>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
Please don't use .GetAll() , it'll be cause for performance issue. it means you are loading all employee records to the application side, then you are actually querying to loaded employee data. just think about if you have 50K+ employee. So, you have to use LINQ IQueryable extension methods, which will generate the SQL operation based on your design query.
Help Link:.NET Entity Framework - IEnumerable VS. IQueryable
Please Try This, if you want to do some other expressions.
context.Employee.Select(x => x.EmployeeId).DefaultIfEmpty(0).Max()+1
If your model have property EmployeeId then set this value to it like below.
public IActionResult Create()
{
var model = new Employee();
model.EmployeeId = _context.GetAll().Max(Employee => Employee.EmployeeId).FirstOrDefault() + 1;
return View(model);
}
Or if you don't have Model to pass to View then use ViewBag.
ViewBag["maxEmployeeId"] = _context.GetAll().Max(Employee => Employee.EmployeeId).FirstOrDefault() + 1;

RowVersion value is not getting binded in form data, mvc 5

I am trying to implement concurrency using EF6 in MVC 5.
#Html.HiddenFor(model => model.RowVersion)
On my edit page I am able to see the rowversion value in input type hidden.
<input id="RowVersion" name="RowVersion" type="hidden" value="AAAAAAAAF3M=">
But on $('form').serializeArray() I am not getting RowVersion data, on posting the form also I am getting null value of RowVersion property.
I had added RowVersion column in database table later and updated the edmx after that, I have set concurrency mode to fixed in the primary key column property my table in edmx.
Is there something extra that needs to be done for rowversion ?
Any help would be appreciated.
Update : adding html code
Jquery : I am checking it in console using : $('form').serializeArray()
html output from browser :
<form action="/Master/EditBookMaster/13" method="post"><input name="__RequestVerificationToken" type="hidden" value="Y04ae_LHgfG9Tw9hy2TcHIYbxk_EX_vykyphV7Sm9Wwiz6_f8PpGUY2SULyiZbCdJv4fgBloOlx_QRUz1FQNvXTZUorLt6_EvA9XLxcFsxbQqUlmY9XOCduHa__q1kdRQJpFAx4wOuj5tRu48TLh9A2" /> <div class="form-horizontal">
<h4>BookMaster</h4>
<hr />
<input data-val="true" data-val-number="The field BookMasterId must be a number." data-val-required="The BookMasterId field is required." id="BookMasterId" name="BookMasterId" type="hidden" value="13" />
<div class="form-group">
<label class="control-label col-md-2" for="BookName">BookName</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="BookName" name="BookName" type="text" value="C Programming" />
<span class="field-validation-valid text-danger" data-valmsg-for="BookName" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Count">Count</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-number="The field Count must be a number." data-val-required="The Count field is required." id="Count" name="Count" type="number" value="10" />
<span class="field-validation-valid text-danger" data-valmsg-for="Count" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Publisher">Publisher</label>
<div class="col-md-10">
<input class="form-control text-box single-line" id="Publisher" name="Publisher" type="text" value="Dennis-Ritchie" />
<span class="field-validation-valid text-danger" data-valmsg-for="Publisher" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Subject">Subject</label>
<div class="col-md-10">
<select class="form-control text-box single-line" data-val="true" data-val-number="The field SubjectId must be a number." data-val-required="The SubjectId field is required." id="SubjectId" name="SubjectId"><option value="1">Fiction</option>
<option value="2">Biography</option>
<option value="3">Science</option>
<option value="4">Research</option>
<option selected="selected" value="5">Software developement</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="SubjectId" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="AvailableCount">AvailableCount</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-number="The field AvailableCount must be a number." id="AvailableCount" name="AvailableCount" type="number" value="8" />
<span class="field-validation-valid text-danger" data-valmsg-for="AvailableCount" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
Back to List
</div>
<input id="RowVersion" name="RowVersion" type="hidden" value="AAAAAAAAF3M=" />
$(":input,:hidden").serialize();
code instead of
$('form').serializeArray()

Input validation generating data-* attributes ONLY on Primary key and checkbox type input tags

I've created a sample ASP.NET MVC Core 1.1 web app created using VS2015-Update3. It generates input tags with data -* attributes only on a model property that is a Primary Key, and on a property that is of type bool. For instance, in the following example the generated html (shown below) is showing the data-attributes only on the input tag generated for properties MyEntityId and Prop2 of the model. NOTE: I'm using the default ASP.NET Core Web Application template that automatically installs Jquery, Bootstrap, etc.
Model:
public class MyEntity
{
public int MyEntityId { get; set; }
public string Prop1 { get; set; }
[Column(TypeName = "char(2)")]
[RegularExpression(#"^[0-9]{2,2}$", ErrorMessage = "Must enter two digit numbers"), StringLength(2)]
public string TestCode { get; set; }
public DateTime? StartDate { get; set; }
public bool Prop2 { get; set; }
}
View
<form asp-controller="TestController" asp-action="TestAction" method="post" class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div><input type="hidden" asp-for="MyEntityId" /></div>
<div class="form-group">
<label asp-for="Prop1" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Prop1" class="form-control" style="margin-top:15px;" />
<span asp-validation-for="Prop1" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="TestCode" class="col-md-2 control-label"></label>
<div class="col-md-2">
<input asp-for="TestCode" class="form-control" />
<span asp-validation-for="TestCode" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="StartDate" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="StartDate" type="date" class="form-control" />
<span asp-validation-for="StartDate" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Prop2" class="col-md-2 control-label"></label>
<div class="col-md-1">
<input asp-for="Prop2" class="form-control" style="zoom:0.5;margin-top:25px;" />
<span asp-validation-for="Prop2" class="text-danger"></span>
</div>
</div>
<button type="submit" name="submit" value="testVal">Save</button>
</form>
Generated Html:
<div><input type="hidden" data-val="true" data-val-required="The MyEntityId field is required." id="MyEntityId" name="MyEntityId" value="54321"></div>
<div class="form-group">
<label class="col-md-2 control-label" for="StateName">Prop1</label>
<div class="col-md-2">
<input class="form-control" readonly="" type="text" id="Prop1" name="Prop1" value="TestVal">
<span class="text-danger field-validation-valid" data-valmsg-for="Prop1" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="TestCode">TestCode</label>
<div class="col-md-2">
<input class="form-control" type="text" id="TestCode" name="TestCode" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="TestCode" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="StartDate">Start Date</label>
<div class="col-md-10">
<input type="date" class="form-control" id="StartDate" name="StartDate" value="2015-10-01">
<span class="text-danger field-validation-valid" data-valmsg-for="StartDate" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="Prop2">Test Prop2</label>
<div class="col-md-1">
<input class="form-control" data-val="true" data-val-required="Test Prop2 is required." id="Prop2" name="Prop2" style="zoom:0.5;margin-top:25px;" type="checkbox" value="true">
<span class="text-danger field-validation-valid" data-valmsg-for="Prop2" data-valmsg-replace="true"></span>
</div>
</div>
I'm assuming your issue is that TestCode does not have validation on it, despite having both RegularExpression and StringLength attributes applied. MyEntityId and Prop2 are non-nullable, so there's an implicit required validation for those, while Prop1 and StartDate are nullable and do not have any explicit validation applied. As a result, those are rightly not validated.
TestCode is weird, though. I'm not sure you'd actually get data-* attributes, as both the regular expression and string length can be satisfied using the HTML attributes pattern and maxlength, respectively. But, you should then have pattern and maxlength applied to the input for TestCode which is not the case.
According to the docs, the code you have should work, and at the least, it seems that StringLength is applied via data-val-maxlength rather than (or perhaps in addition to) using the maxlength attribute. There's either some bug at play, or there's something else in your codebase that is preventing the correct behavior from occurring. However, without more code, it's impossible to say.

JQuery UI DatePicker Covering Controls Below

I have a set of two date fields where the JQuery UI DatePicker is covering up the controls below. How do I set the ZIndex or Position of the DatePicker so it does not collide with the controls? I am using FireFox.
Here is my JSFiddle:
http://jsfiddle.net/pNP22/4/
Here is my HTML:
<div class="editor-label">
<label for="StartDate">Choose Start Date</label>
</div>
<div class="editor-field">
<input id="StartDate" name="StartDate" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="StartDate" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="EndDate">Choose End Date</label>
</div>
<div class="editor-field">
<input id="EndDate" name="EndDate" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="EndDate" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="SiteLocation">Choose Site Location</label>
</div>
<div class="editor-field">
<select data-val="true" data-val-required="The Choose Site Location field is required." id="SiteLocation" name="SiteLocation"><option value="">-- Please Select --</option>
<option value="fce39672-cf3b-40c4-bae9-a0e800fb193c">Columbus, US</option>
<option value="ed567544-ea5e-4cb7-9be7-a2030040e017">Granada</option>
<option value="8e9630b4-db21-4a35-9894-a17e000b4eb7">Singapore</option>
</select>
</div>
<div class="editor-label">
<label for="RequestNumber">Request #</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Request # must be a number." id="RequestNumber" name="RequestNumber" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="RequestNumber" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Commodity">Commodity</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="Commodity" name="Commodity" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Commodity" data-valmsg-replace="true"></span>
</div>
Here is my Javascript:
$('#StartDate').datepicker();
$('#EndDate').datepicker();
This seems to work: http://jsfiddle.net/pNP22/6/
$('#EndDate').datepicker({
beforeShow: function (input, inst) {
inst.dpDiv.css({
marginTop: -input.offsetHeight + 'px',
marginLeft: input.offsetWidth + 'px'
});
}
});
Note the change is global, once executed on one picker, the other picker starts to use it.
I stole it from here: How to change the pop-up position of the jQuery DatePicker control

MVC view with two forms, why are both rendering to same action?

#using (Html.BeginForm("Unassign", "Terminals_DataGroups"))
{
#Html.HiddenFor(m => m.DeviceId)
#Html.HiddenFor(m => m.DataGroupId)
<input type="submit" value="Remove group from device" #(string.IsNullOrEmpty(Model.DataGroupName) ? "disabled=\"disabled\"" : "") />
}
#using (Html.BeginForm("Assign", "Terminals_DataGroups"))
{
#Html.HiddenFor(m => m.DeviceId)
<div class="editor-label">
#Html.LabelFor(model => model.DataGroups)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.DataGroupsSelectedItem, new SelectList(Model.DataGroups, "Value", "Text"))
#Html.ValidationMessageFor(model => model.DataGroupsSelectedItem)
</div>
<input type="submit" value="Assign Data Group" />
}
Yet the HTML renders as:
<form action="/Terminals_DataGroups/Assign" method="post">
<input data-val="true"
data-val-number="The field DeviceId must be a number."
data-val-required="The DeviceId field is required."
id="DeviceId"
name="DeviceId"
type="hidden"
value="15" />
<input data-val="true"
data-val-number="The field DataGroupId must be a number."
id="DataGroupId"
name="DataGroupId"
type="hidden"
value="1" />
<input type="submit" value="Remove group from device" />
</form>
<form action="/Terminals_DataGroups/Assign" method="post">
<input data-val="true"
data-val-number="The field DeviceId must be a number."
data-val-required="The DeviceId field is required."
id="DeviceId"
name="DeviceId"
type="hidden"
value="15" />
<div class="editor-label">
<label for="DataGroups">Data groups</label>
</div>
<div class="editor-field">
<select data-val="true"
data-val-number="The field DataGroupsSelectedItem must be a number."
id="DataGroupsSelectedItem"
name="DataGroupsSelectedItem">
<option value="2">DataGroup2</option>
<option value="3">DataGroup3</option>
<option value="5">New group</option>
</select>
<span class="field-validation-valid"
data-valmsg-for="DataGroupsSelectedItem"
data-valmsg-replace="true"></span>
</div>
<input type="submit" value="Assign Data Group" />
</form>
you must added ID tag and value
sory my english ;)

Resources