gpt4 book ai didi

带有文件类型和文本的表单上的 C# asp.net 问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:12 25 4
gpt4 key购买 nike

我要开门见山了,

我有一个表格。当我保存表格时......它只得到名字,中间名和姓氏......它没有得到文件......但是,如果我只得到照片并注释掉其他输入......照片是在我的模型..我不知道为什么它会这样..我真的是asp.net mvc的新手..所以请耐心等待..

@model Impulse.ViewModels.AgentViewModel

@{
ViewBag.Title = "AgentForm";
Layout = "~/Views/Shared/_SiteLayout.cshtml";
}

<div class="custom-container">
<h1 class="title"><strong>Add New Agent</strong></h1>
<hr />
@using (Html.BeginForm("Save", "Agent", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<div class="row">
<div class="col-md-3">
<div id="preview">
<img src="~/Content/Assets/no-image.png" id="profile_image" class="img-thumbnail" />
</div>
<div class="form-group">
<label>Profile Picture</label>
<input type="file" name="photo" id="photo" />
</div>
</div>
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(m => m.Agent.FirstName)
@Html.TextBoxFor(m => m.Agent.FirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Agent.FirstName)
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(m => m.Agent.MiddleName)
@Html.TextBoxFor(m => m.Agent.MiddleName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Agent.MiddleName)
</div>
</div>
</div>

<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(m => m.Agent.LastName)
@Html.TextBoxFor(m => m.Agent.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Agent.LastName)
</div>
</div>
</div>
</div>
</div>


<input type="submit" class="btn btn-primary" value="Save" />
}


</div>

Controller

   [HttpPost]
public ActionResult Save(AgentModel agent)
{
//I debug here to see the data passed by my view
return Content("Sample");
}

型号

public class AgentModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
[FileSize(10240)]
[FileTypes("jpg,jpeg,png")]
public HttpPostedFileBase photo { get; set; }

}

最佳答案

你可以这样试试

型号

public class UploadFileModel 
{
public UploadFileModel()
{
Files = new List<HttpPostedFileBase>();
}

public List<HttpPostedFileBase> Files { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}

查看

@using (Html.BeginForm("UploadData", "Home", FormMethod.Post, new { encType="multipart/form-data" }))
{
@Html.TextBoxFor(m => m.FirstName)
<br /><br />

@Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br /><br />
<input type="submit" value="submit" name="submit" id="submit" />
}

Controller

public ActionResult UploadData(UploadFileModel model)
{
var file = model.Files[0];
return View(model);
}

关于带有文件类型和文本的表单上的 C# asp.net 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com