繁体   English   中英

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

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

我要直说到这里,伙计们,

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

控制者

   [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" />
}

控制者

public ActionResult UploadData(UploadFileModel model)
{
    var file = model.Files[0];
    return View(model);
}
  1. 您将绑定到AgentViewModel的视图,因此发布服务器时将获得AgentViewModel。 因此要保存的参数应该是viewmodel。 或其他更改视图以绑定到AgentModel。
  2. 您使用的文件控件是html输入类型。 尝试使用下面的代码。

@ 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