MVC 控制器在将模型传递给控制器时给我一个奇怪的情况
MVC controller giving me strange situation when passing model to controller
下图是在调试过程中您可以看到控制器接收到正确的 id 但图像 = null,这很好,因为可以看到我的 htm.beginform() 没有处理文件请求并且我的代码可以正常工作,除了图像未保存到数据库
在下面的第二张图片中,当我将代码添加到 htmbeginform() 中以便它处理文件上传时(顺便说一句,我在创建新广告时使用了相同的代码并且它起作用了)出于某种原因模型 returns id=0 虽然它现在有图像数据,但是因为 id=0 广告没有得到更新因为那不是广告 id
我试过使用 linq,但遇到了同样的问题,我不明白为什么在其余模型数据正常的情况下继续获取 id=0
我正在使用存储库模式,因此我的业务逻辑在另一层并使用 maodel 传递数据,但我认为这不是问题
@model Template.Model.AdvertModel
<h1>Update a tender Advert View</h1>
<br />
@using (Html.BeginForm("UpdateAdvert", "Manager", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Advert details</legend>
<div class="row">
@Html.LabelFor(model => model.TenderId, "Tender Ref Number:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderId)
@Html.ValidationMessageFor(model => model.TenderId, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderTitle, "Title:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderTitle, 5, 55, null)
@Html.ValidationMessageFor(model => model.TenderTitle, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.Description, "Desription:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.Description, 10, 55, null)
</div><br />
@*<div class="row">
@Html.LabelFor(model => model.TenderSector, "Tender Sector:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderSector)
</div><br/>*@
<div class="row">
@Html.LabelFor(model => model.TenderSector, new { @class = "control-label col-md-3" })
@Html.DropDownListFor(m => m.TenderSector, new List<SelectListItem>
{
new SelectListItem {Text = "Roads", Value = "Roads"},
new SelectListItem {Text = "Building Construction", Value = "Building Construction"},
new SelectListItem {Text = "Maintainance", Value = "Maintainance"},
new SelectListItem {Text = "Bridges", Value = "Bridges"},
new SelectListItem {Text = "Other", Value = "Other"}
}, "--Select Option--", new { @class = "control-label col-md-2" })
@Html.ValidationMessageFor(m => m.TenderSector, "", new { @class = "text-danger" })
</div>
<br />
<div class="row">
@Html.LabelFor(model => model.TenderIssuer, "Tender Issuer:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderIssuer)
@Html.ValidationMessageFor(model => model.TenderIssuer, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderMeetingDateTime, "Tender Meeting date and time:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderMeetingDateTime, new { htmlAttributes = new { @class = "date form_datetime" } })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderMeetingVenue, "Meeting Venue:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderMeetingVenue, 5, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.CompulsoryMeeting, "Compulsory To Attend Meting:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.CompulsoryMeeting)
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderContactDetails, "Tender Contact Details:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderContactDetails, 5, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocPickUp, "Document Pickup Location:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.DocPickUp, 5, 55, null)
@Html.ValidationMessageFor(model => model.DocPickUp, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocDropOff, "Document Drop Off Location:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.DocDropOff, 5, 55, null)
@Html.ValidationMessageFor(model => model.DocDropOff, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocCost, "Document Cost: R", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.DocCost)
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderCloseDate, "Date Submission closes:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderCloseDate, new { htmlAttributes = new { @class = "date form_datetime" } })
@Html.ValidationMessageFor(model => model.TenderCloseDate, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.OtherInfo, "Other Information:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.OtherInfo, 10, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.Active, "Activate Advert (Visible to site visitors):", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.Active)
</div><br/>
<div class="row"></div><br/>
@if (Model.Image != null)
{
<div class="row">
<img src="data:image;base64,@System.Convert.ToBase64String(Model.Image)" max-width="100%" max-height="100%"/>
<br/>
@Html.LabelFor(model => model.Image, "Change Image", new {@class = "control-label col-md-3"})
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
</div><br/>
}
@if (Model.Image == null)
{
<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
}
@*<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file" />
</div>*@
<hr />
@*<input type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" value="Create new advert" data-style="expand-right" />*@
<button type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" data-style="expand-right">
<span class="ladda-label"> Update Advert </span>
</button>
</fieldset>
}
<br />
在 BeginForm()
中为您的 model.Id
添加一个隐藏字段
@Html.HiddenFor(model=> model.Id)
下图是在调试过程中您可以看到控制器接收到正确的 id 但图像 = null,这很好,因为可以看到我的 htm.beginform() 没有处理文件请求并且我的代码可以正常工作,除了图像未保存到数据库
在下面的第二张图片中,当我将代码添加到 htmbeginform() 中以便它处理文件上传时(顺便说一句,我在创建新广告时使用了相同的代码并且它起作用了)出于某种原因模型 returns id=0 虽然它现在有图像数据,但是因为 id=0 广告没有得到更新因为那不是广告 id
我试过使用 linq,但遇到了同样的问题,我不明白为什么在其余模型数据正常的情况下继续获取 id=0
我正在使用存储库模式,因此我的业务逻辑在另一层并使用 maodel 传递数据,但我认为这不是问题
@model Template.Model.AdvertModel
<h1>Update a tender Advert View</h1>
<br />
@using (Html.BeginForm("UpdateAdvert", "Manager", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Advert details</legend>
<div class="row">
@Html.LabelFor(model => model.TenderId, "Tender Ref Number:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderId)
@Html.ValidationMessageFor(model => model.TenderId, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderTitle, "Title:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderTitle, 5, 55, null)
@Html.ValidationMessageFor(model => model.TenderTitle, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.Description, "Desription:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.Description, 10, 55, null)
</div><br />
@*<div class="row">
@Html.LabelFor(model => model.TenderSector, "Tender Sector:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderSector)
</div><br/>*@
<div class="row">
@Html.LabelFor(model => model.TenderSector, new { @class = "control-label col-md-3" })
@Html.DropDownListFor(m => m.TenderSector, new List<SelectListItem>
{
new SelectListItem {Text = "Roads", Value = "Roads"},
new SelectListItem {Text = "Building Construction", Value = "Building Construction"},
new SelectListItem {Text = "Maintainance", Value = "Maintainance"},
new SelectListItem {Text = "Bridges", Value = "Bridges"},
new SelectListItem {Text = "Other", Value = "Other"}
}, "--Select Option--", new { @class = "control-label col-md-2" })
@Html.ValidationMessageFor(m => m.TenderSector, "", new { @class = "text-danger" })
</div>
<br />
<div class="row">
@Html.LabelFor(model => model.TenderIssuer, "Tender Issuer:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderIssuer)
@Html.ValidationMessageFor(model => model.TenderIssuer, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderMeetingDateTime, "Tender Meeting date and time:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderMeetingDateTime, new { htmlAttributes = new { @class = "date form_datetime" } })
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderMeetingVenue, "Meeting Venue:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderMeetingVenue, 5, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.CompulsoryMeeting, "Compulsory To Attend Meting:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.CompulsoryMeeting)
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderContactDetails, "Tender Contact Details:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.TenderContactDetails, 5, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocPickUp, "Document Pickup Location:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.DocPickUp, 5, 55, null)
@Html.ValidationMessageFor(model => model.DocPickUp, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocDropOff, "Document Drop Off Location:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.DocDropOff, 5, 55, null)
@Html.ValidationMessageFor(model => model.DocDropOff, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.DocCost, "Document Cost: R", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.DocCost)
</div><br />
<div class="row">
@Html.LabelFor(model => model.TenderCloseDate, "Date Submission closes:", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.TenderCloseDate, new { htmlAttributes = new { @class = "date form_datetime" } })
@Html.ValidationMessageFor(model => model.TenderCloseDate, null, new { @class = "text-danger" })
</div><br />
<div class="row">
@Html.LabelFor(model => model.OtherInfo, "Other Information:", new { @class = "control-label col-md-3" })
@Html.TextAreaFor(model => model.OtherInfo, 10, 55, null)
</div><br />
<div class="row">
@Html.LabelFor(model => model.Active, "Activate Advert (Visible to site visitors):", new { @class = "control-label col-md-3" })
@Html.EditorFor(model => model.Active)
</div><br/>
<div class="row"></div><br/>
@if (Model.Image != null)
{
<div class="row">
<img src="data:image;base64,@System.Convert.ToBase64String(Model.Image)" max-width="100%" max-height="100%"/>
<br/>
@Html.LabelFor(model => model.Image, "Change Image", new {@class = "control-label col-md-3"})
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
</div><br/>
}
@if (Model.Image == null)
{
<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file"/>
</div>
}
@*<div class="editor-label">
Image
</div>
<div class="editor-field">
<input type="file" name="file" id="file" />
</div>*@
<hr />
@*<input type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" value="Create new advert" data-style="expand-right" />*@
<button type="submit" style="margin-left: 50%" class="btn btn-sky text-uppercase btn-lg ladda-button" data-style="expand-right">
<span class="ladda-label"> Update Advert </span>
</button>
</fieldset>
}
<br />
在 BeginForm()
中为您的 model.Id
@Html.HiddenFor(model=> model.Id)