微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Asp.net Core 2.1上使用Boostrap Modal上传文件

如何解决在Asp.net Core 2.1上使用Boostrap Modal上传文件

我想上传文件并发送到电子邮件。但是我看不到如何用Bootsrap Modal做到这一点。

如果您需要其他任何信息,请告诉我。

我想通过电子邮件发送附件,然后将其集成到数据库中并能够下载。

/ [...] / ==>我没有添加代码,没有用。

编辑:否则如何使用Ajax?

我的观点:

<form name="contact" method="post" asp-action="Validation">
@if (ViewBag.Alert != null && ViewBag.Alert != "")
{
    <div class="alert role="alert">
        @ViewBag.Alert
    </div>
}
<div class="details">
    <h3 class="title">Demande</h3>
    <label for="card">Type *</label>
    <select required onchange="contact()" class=" form-control" disabled="true" name="type" value="@ViewBag.Form.Type">
        <option value="1">Choose 1</option>
        <option value="2">Choose 2</option>
    </select>
    <label for="card">Origine *</label>
    <select required class="form-control" name="origine" value="@ViewBag.Form.Origine">
        <option value="1">Origine 1</option>
        <option value="2">Origine 2</option>
        <option value="3">Origine 3</option>
    </select>
    /*[...]*/
    <input type="hidden" name="Id" value="@ViewBag.Form.Id" />
    <input type="hidden" name="Price" value="@ViewBag.Form.Price" />
    /*[...]*/
    <textarea class="form-control" name="Response" placeholder="Response..."></textarea>
    <button required class="btn" onclick="modal_response()" data-whatever="getbootstrap" data-toggle="modal" data-target="#modal_contact" type="button">Response</button>
    <label for="card" class="right">Response send</label><br />
    <button required class="btn_second" onclick="modal_save()" data-whatever="getbootstrap" data-toggle="modal" data-target="#modal_contact_save" type="button">Save</button>
    <label for="card-holder" class="right">Save Ok!</label>
    /*[...]*/
    <div class="form-row">
        <button class="btn-primary" onclick="form_confirmation()" type="submit">Save All</button>
        <button class="btn-secondary" type="reset" onclick="location.href = '@Url.Action("List","Control",new { id = ViewBag.Id })';">Close</button>
    </div>
</div>
</form>

/* When I click on "Response" a new window open */

<div class="modal fade" id="modal_contact" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <h5 class="modal-title" id="modal_label">Send email to : @ViewBag.Form.Name</h5>
            <button type="button" class="close" data-dismiss="modal"></button>
    </div>
    <div class="modal-body">
        <form id="modal_contact_form" enctype = "multipart/form-data" method="post" action="@Url.Action("Modal_contact","Control")">
            <div class="form-group">
                <input type="file" name="file" accept="application/pdf" /><br /><br />
                <textarea class="form-control" name="Description" required placeholder="Content">@ViewBag.Response</textarea>
            </div>
            <input type="hidden" name="Id" value="ID" hidden />
            <input type="hidden" name="Price" value="@ViewBag.Form.Price" hidden />
            <input type="hidden" name="Type" value="@ViewBag.Form.Type" hidden />
            /*[...]*/
            <input type="submit" hidden />
        </form>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" data-dismiss="modal" onclick="$('#modal_contact_form').submit();" class="btn-primary">Send Email</button>
    </div>
</div>

我的控制器:

public class MyController : BaseEntity
{
    [required]
    public string Id { get; set; }
    public string Price { get; set; }
    public string Type { get; set; }
    /*[...]*/

    /* For File I don't kNow if it's good?? */
    public IformFile File { get; set; }
}

我的模特:

public ActionResult Modal_contact(MyController model)
{
    /* How to retrieve file ?? */
    bool ok = false;
    OtherController OC = new OtherController();
    /*[...]*/
    if (ModelState.IsValid)
    {
        OC = othercontrollerRepository.Find(model.Id);
        OC.Email = this.Session_get("Email");
        OC.Description = "";
        model.Email = this.Session_get("Email");
        model.Status = "1";
        model.View = "response";
        /*[...]*/
        if (mycontrollerRepository.Add(model) && othercontrollerRepository.Update(OC))
        {
            /* How to send file to email and save file to folder in my project to add in my database ? */
            MailManager mail = new MailManager();
            Modele modele = new Modele();
            modele= modeleRepository.Find("response");

            ok = mail.send(modele.Objet,model.Description,OC.Email);
        }
               
        return Json(new { Json_result = ok,Redirect = "" });
    }   
        return Json(new { Json_result = false,Redirect = "" });
    }

我发现了我的错误Form之前的<div>

解决方法

我尝试了您的代码并转载了您的问题。我注意到您将IFormFile对象命名为File。

public IFormFile File { get; set; }

但是您得到的输入文件名为文件。

<input type="file" name="file" accept="application/pdf"/><br /><br />

Asp.net核心模型绑定将根据输入名称绑定属性。这就是为什么您无法在控制器中获取IFormFile值的原因。

要解决此问题,您应该将输入的名称更改为File,以便它们可以相互匹配。

像下面这样:

<input type="file" name="File" accept="application/pdf"/><br /><br />

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。