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

标签 c# asp.net asp.net-mvc

我要开门见山了,

我有一个表格。当我保存表格时......它只得到名字,中间名和姓氏......它没有得到文件......但是,如果我只得到照片并注释掉其他输入......照片是在我的模型..我不知道为什么它会这样..我真的是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/

相关文章:

c# - 具有 Oracle 数组绑定(bind)的 ExecuteReader

c# - 任务取消暂停 UI

c# - 冒号( :) in url causes error in asp. 网络

asp.net-mvc - 在 Visual Studio 中卸载项目有什么意义?

Ajax.BeginForm 忽略 MVC 5 中参数中定义的 Action 和 Controller

asp.net-mvc - 如何在 ASP.NET MVC 中配置 HTML 缩小

c# - XML 序列化和命名空间前缀

c# - 如何有效读取流上 JPEG 图像的大小

c# - 如何在 asp.net core blazor 的通用服务中使用任何模型

asp.net - SqlDependency 与 SqlCacheDependency