使用 mvc 中的文件名表单文件输入标记将数据从视图传递到控制器
Pass Data from view to controller with file name form file input tag in mvc
我有一个 mvc 应用程序,因为我创建了一个用户可以在线填写的表格。在那种形式下,我为用户的照片使用了多个文本框和输入文件标签。我也创建了模型,首先看到这个 Sampleform.cshtml 视图
@model OnlineFilling.Models.ConsumerModel
<div class="row">
<div class="row" style="width:90%; margin-left:45px">
<div class="row">
<div class="row">
<div class="row" >
<div class="col-sm-3"><h4>Consumer Name:</h4> </div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtConsumerFirstName" placeholder="First Name" style="width:200px;"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Father/Husband/WifeName:</h4> </div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtParentsFirstName" placeholder="First Name" style="width:200px"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Mother Name:</h4></div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtMotherFirstName" placeholder="First Name" style="width:200px"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Upload photo And Signature</h4></div>
<div class="col-sm-3">
<fieldset class="scheduler-border" style="background-color:#fff">
<legend class="scheduler-border">Consumer Photo</legend>
<input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
</fieldset>
</div>
<div class="col-sm-3">
<fieldset class="scheduler-border" style="background-color:#fff">
<legend class="scheduler-border">Consumer Sign</legend>
<input type="file" id="ConsumerSign" value="Signature" />
</fieldset>
</div>
</div>
</div>
<br />
.
.
.
.
<div class="row">
<div class="col-sm-2"><h4>Account No</h4></div>
<div class="col-sm-2"><input type="text" id="txtAccount" class="input-sm" /></div>
<div class="col-sm-1"> <h4>Bank I.f.S Code</h4></div>
<div class="col-sm-2"><input type="text" id="txtIFS" class="input-sm" /></div>
</div>
</div>
</div>
</div>
</div>
</form>
那么这是我的 HomeController
[HttpPost]
public ActionResult SampleForm(ConsumerModel objModel, int ConID, string consumerfN, string ConGaurdian, string ConMother, int AcntNum, string conIfS)
{
var ImgfileName = Path.GetFileName(objModel.file1.FileName);
WebImage img = new WebImage(ImgfileName);
string imageName = ConID + "_" + consumerfN + "_" + conCity+"_ProfPic";
var path = Path.Combine(Server.MapPath("~/FileManager"), imageName);
if (img.Width > 450 || img.Width<450)
{
img.Resize(450, 450);
img.Save(path);
}
img = new WebImage(ConSign.InputStream);
string SignName = ConID + "_" + consumerfN + "_" + conCity + "Signture";
var SignPath = Path.Combine(Server.MapPath("~/FileManager"), imageName);
if (img.Width > 800 || img.Height > 300)
{
img.Resize(800, 300);
img.Save(SignPath);
}
consumerModel.ConsumerFName = consumerfN;
consumerModel.GaurdianName = ConGaurdian;
consumerModel.MotherName = ConMother;
consumerModel.AccountNo = AcntNum;
consumerModel.IFScode = conIfS;
consumerModel.PhotoPath = path;
consumerModel.Signature = SignPath;
bool TotalInfo = Middleclass.GetConsumerInfo(consumerModel);
return RedirectToAction("SampleForm");
}
这是我的模特
public class ConsumerModel
{
public int Id { get; set; }
public string ConsumerFName { get; set; }
public string ConsumerMName { get; set; }
public string ConsumerLName { get; set; }
public string GaurdianName { get; set; }
public string MotherName { get; set; }
public string Qualification { get; set; }
public System.DateTime DOB { get; set; }
public int Age { get; set; }
public string MarriedStatus { get; set; }
public string BloodGroup { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string District { get; set; }
public string State { get; set; }
public int PINcode { get; set; }
public Nullable<int> Telephone { get; set; }
public int Mobile { get; set; }
public string E_Mail { get; set; }
public string PANno { get; set; }
public string AdharNumber { get; set; }
public string AvailableOn { get; set; }
public int MobileNumberAv { get; set; }
public string CompanyGrp { get; set; }
public int AccountNo { get; set; }
public string IFScode { get; set; }
public string BankAddr { get; set; }
public string PhotoPath { get; set; }
public string Signature { get; set; }
public string Gender { get; set; }
public string Nationality { get; set; }
public HttpPostedFileBase file1 { get; set; }
public HttpPostedFileBase file2 { get; set; }
}
但最后我使用 ajax 调用来发送数据它会正确执行但它没有将文件标签值发送到控制器,然后我使用
@using (Html.BeginForm("Sampleform", "home", FormMethod.Post, new {enctype="multipart/form-data"}))
然后它会引发此错误,例如..
所以请你建议我解决这个错误......................?????
我希望你从 7 月开始就解决了你的问题,但我 post 我对下一个问题的回答:
我通过在输入字段添加名称标签解决了这个问题。
尝试:
<input type="file" id="ConsumerPic" value="Photo" name="file" onchange="getfilename(this);"/>
而不是:
<input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
我希望这会有所帮助 :)
我有一个 mvc 应用程序,因为我创建了一个用户可以在线填写的表格。在那种形式下,我为用户的照片使用了多个文本框和输入文件标签。我也创建了模型,首先看到这个 Sampleform.cshtml 视图
@model OnlineFilling.Models.ConsumerModel
<div class="row">
<div class="row" style="width:90%; margin-left:45px">
<div class="row">
<div class="row">
<div class="row" >
<div class="col-sm-3"><h4>Consumer Name:</h4> </div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtConsumerFirstName" placeholder="First Name" style="width:200px;"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Father/Husband/WifeName:</h4> </div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtParentsFirstName" placeholder="First Name" style="width:200px"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Mother Name:</h4></div>
<div class="col-sm-2"><input type="text" class="input-sm" id="txtMotherFirstName" placeholder="First Name" style="width:200px"/></div>
</div>
<br />
<div class="row">
<div class="col-sm-15">
<div class="col-sm-3"><h4>Upload photo And Signature</h4></div>
<div class="col-sm-3">
<fieldset class="scheduler-border" style="background-color:#fff">
<legend class="scheduler-border">Consumer Photo</legend>
<input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
</fieldset>
</div>
<div class="col-sm-3">
<fieldset class="scheduler-border" style="background-color:#fff">
<legend class="scheduler-border">Consumer Sign</legend>
<input type="file" id="ConsumerSign" value="Signature" />
</fieldset>
</div>
</div>
</div>
<br />
.
.
.
.
<div class="row">
<div class="col-sm-2"><h4>Account No</h4></div>
<div class="col-sm-2"><input type="text" id="txtAccount" class="input-sm" /></div>
<div class="col-sm-1"> <h4>Bank I.f.S Code</h4></div>
<div class="col-sm-2"><input type="text" id="txtIFS" class="input-sm" /></div>
</div>
</div>
</div>
</div>
</div>
</form>
那么这是我的 HomeController
[HttpPost]
public ActionResult SampleForm(ConsumerModel objModel, int ConID, string consumerfN, string ConGaurdian, string ConMother, int AcntNum, string conIfS)
{
var ImgfileName = Path.GetFileName(objModel.file1.FileName);
WebImage img = new WebImage(ImgfileName);
string imageName = ConID + "_" + consumerfN + "_" + conCity+"_ProfPic";
var path = Path.Combine(Server.MapPath("~/FileManager"), imageName);
if (img.Width > 450 || img.Width<450)
{
img.Resize(450, 450);
img.Save(path);
}
img = new WebImage(ConSign.InputStream);
string SignName = ConID + "_" + consumerfN + "_" + conCity + "Signture";
var SignPath = Path.Combine(Server.MapPath("~/FileManager"), imageName);
if (img.Width > 800 || img.Height > 300)
{
img.Resize(800, 300);
img.Save(SignPath);
}
consumerModel.ConsumerFName = consumerfN;
consumerModel.GaurdianName = ConGaurdian;
consumerModel.MotherName = ConMother;
consumerModel.AccountNo = AcntNum;
consumerModel.IFScode = conIfS;
consumerModel.PhotoPath = path;
consumerModel.Signature = SignPath;
bool TotalInfo = Middleclass.GetConsumerInfo(consumerModel);
return RedirectToAction("SampleForm");
}
这是我的模特
public class ConsumerModel
{
public int Id { get; set; }
public string ConsumerFName { get; set; }
public string ConsumerMName { get; set; }
public string ConsumerLName { get; set; }
public string GaurdianName { get; set; }
public string MotherName { get; set; }
public string Qualification { get; set; }
public System.DateTime DOB { get; set; }
public int Age { get; set; }
public string MarriedStatus { get; set; }
public string BloodGroup { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string District { get; set; }
public string State { get; set; }
public int PINcode { get; set; }
public Nullable<int> Telephone { get; set; }
public int Mobile { get; set; }
public string E_Mail { get; set; }
public string PANno { get; set; }
public string AdharNumber { get; set; }
public string AvailableOn { get; set; }
public int MobileNumberAv { get; set; }
public string CompanyGrp { get; set; }
public int AccountNo { get; set; }
public string IFScode { get; set; }
public string BankAddr { get; set; }
public string PhotoPath { get; set; }
public string Signature { get; set; }
public string Gender { get; set; }
public string Nationality { get; set; }
public HttpPostedFileBase file1 { get; set; }
public HttpPostedFileBase file2 { get; set; }
}
但最后我使用 ajax 调用来发送数据它会正确执行但它没有将文件标签值发送到控制器,然后我使用
@using (Html.BeginForm("Sampleform", "home", FormMethod.Post, new {enctype="multipart/form-data"}))
然后它会引发此错误,例如..
所以请你建议我解决这个错误......................?????
我希望你从 7 月开始就解决了你的问题,但我 post 我对下一个问题的回答:
我通过在输入字段添加名称标签解决了这个问题。
尝试:
<input type="file" id="ConsumerPic" value="Photo" name="file" onchange="getfilename(this);"/>
而不是:
<input type="file" id="ConsumerPic" value="Photo" onchange="getfilename(this);"/>
我希望这会有所帮助 :)