简体   繁体   English

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

[英]C# asp.net issue on Form with file type and text

I'm going straight to the point here guys, 我要直说到这里,伙计们,

I have a form. 我有一个表格。 when I save the form... it only gets the firstname, middlename and lastname.. it doesn't get the files... however, if I only get the photo and comment out other inputs... the photo is captured on my model.. I dunno why it behaves like this.. I'm really new to asp.net mvc.. so please bear with me.. 当我保存表单时...仅获取名字,中间名和姓氏..但不获取文件...但是,如果我仅获取照片并注释掉其他输入...则在我的模型..我不知道为什么它会如此.. 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");
    }

Model 模型

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; }

    }

you can try like this 你可以这样尝试

Model 模型

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; }
}

View 视图

@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);
}
  1. you are binding to view to AgentViewModel so you will get AgentViewModel when you post server. 您将绑定到AgentViewModel的视图,因此发布服务器时将获得AgentViewModel。 so the parameter to action save should be viewmodel. 因此要保存的参数应该是viewmodel。 Or Else change view to bind to AgentModel. 或其他更改视图以绑定到AgentModel。
  2. The file control that you have used is html input type. 您使用的文件控件是html输入类型。 try using below code. 尝试使用下面的代码。

@Html.TextBoxFor(m => Model.File, new { type = "file" , accept=".pdf"}) @Html.ValidationMessageFor(m => Model.File) @ Html.TextBoxFor(m => Model.File,新的{type =“ file”,accept =“。pdf”})@ Html.ValidationMessageFor(m => Model.File)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM