这是有效的,现在由于原因不明,我的文件集合为空
查看
@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生成
<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>
控制器:
[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();
}
在控制器的后期动作中,colelction是空的,任何人都可以看到一个明显的错误
答案 0 :(得分:3)
命名问题:
<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" />
当然应该是:
<input class="text-box single-line" type="file" name="files" id="IT application file" data-val="true" data-val-required="Please select a file" />
因此,如果您希望模型绑定器能够绑定到名为files
的操作参数,请确保您的Razor代码并确保已将正确的名称应用于文件输入:
<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" />
啊,id
的方式不能包含空格。所以你有一个破碎的HTML。以下似乎是错误的:id="@pick.Name.Trim()"
。