激活表单中的方法

时间:2015-06-25 07:19:36

标签: c# asp.net-mvc

我想在表单中提交单个文本字段。我有这个观点:

@model SenecaFormsServer.Areas.Dashboard.Models.EditProductModel
@using SenecaFormsServer.Areas.Dashboard.Models
@using Seneca.SfsLib.Connectors

<div id="ConnectorSettings-MAIL_PRODUCT_HANDLERS">
    <h4>@Resources.Entity.Product.SettingsForConnector: Mail product handlers</h4>
    @{
        MailProductHandlersModel mailModel = Model.ConnectorModels["MAIL_PRODUCT_HANDLERS"] as MailProductHandlersModel;
    }

    <div class="form-group">
        @Html.Label(Resources.Entity.Product.Sender, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Editor("MailProductHandlers_sender", new { htmlAttributes = new { @class = "form-control", @Value = mailModel.Sender, @placeholder = Model.CustomerConfig.DefaultSenderAddress } })
        </div>
    </div>

    <div class="form-group">
        @Html.Label(Resources.Entity.Product.MailSubject, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Editor("MailProductHandlers_subject", new { htmlAttributes = new { @class = "form-control", @Value = mailModel.Subject } })
        </div>
    </div>

    <!--
    <div class="form-group">
        @Html.Label("Reply to form submitter", new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.CheckBox("MailProductHandlers_replytoFormSubmitter", new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>
    -->

    <div class="form-group">
        @Html.Label(Resources.Entity.Product.MailMessage, new { @class = "text-bold control-label col-md-2" })
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.EditorFor(model => mailModel.Message, new { htmlAttributes = new { @class = "form-control tiny-mce", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
            @Html.ValidationMessageFor(model => mailModel.Message)
            @*@Html.TextArea("MailProductHandlers_message", mailModel.Message, new { @class = "form-control", @rows = 15 })*@
        </div>
    </div>

    <div class="form-group">
            @Html.Label(Resources.Entity.Product.GeneratePDF, new { @class = "text-bold control-label col-md-2" })

        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.Label(Resources.Entity.Product.GeneratePDFYes) @Html.RadioButtonFor(model => model.IsChecked, true, new { @checked = "checked", @class = "pdfchecker" })
            @Html.Label(Resources.Entity.Product.GeneratePDFNo)  @Html.RadioButtonFor(model => model.IsChecked, false, new { @class = "pdfchecker" })
        </div>
    </div>

        <div class="form-group">
        <div id="hideLable">
            @Html.Label(Resources.Entity.Product.PdfMessage, new { @class = "text-bold control-label col-md-2" })
        </div>
        <div class="col-lg-6 col-md-8 col-sm-10 ">
            @Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { @class = "form-control tiny-mce", @id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
            @Html.ValidationMessageFor(model => mailModel.PdfMessage)
        </div>
    </div>

    @using (Html.BeginForm("GeneratePDFFromHtml", "Product",  FormMethod.Post)) {
        @Html.AntiForgeryToken()
        <div class="col-xs-12 padding-bottom-10">
            @*<a href="@Url.Action("GeneratePDFFromHtml", new { designId = Model.Id })" class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </a>*@
            <button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF</button>           
        </div>

    }

我想提交这部分内容:

  @using (Html.BeginForm("GeneratePDFFromHtml", "Product",  FormMethod.Post)) {
        @Html.AntiForgeryToken()
        <div class="col-xs-12 padding-bottom-10">
            @*<a href="@Url.Action("GeneratePDFFromHtml", new { designId = Model.Id })" class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </a>*@
            <button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF</button>           
        </div>

    }

这是行动方法:

 [HttpPost]
        [ValidateAntiForgeryToken]                
        public ActionResult GeneratePDFFromHtml(EditProductModel model, string data, string designId)
        {

            string customerSchema = SfsHelpers.StateHelper.GetSchema();
            //TemplateLibraryEntry entry =  GetTemplateLibraryEntry(designId, customerSchema);
            SubmittedForm sf = new SubmittedForm();
            string schema = requestSchema;
            customer_DbConnection db = new customer_DbConnection();
            RenderFormController renderController = new RenderFormController();
            renderController.GeneratePdf(data, db,sf);
            //return RedirectToAction(model.DesignId, "Prdocut/Edit");
            return Content("It works");           
        }


</div>

但未触发操作方法。

如何触发操作方法?

已在此文本字段中创建PDF文件:

@using (Html.BeginForm("GeneratePDFFromHtml", "Product", null, FormMethod.Post, new { enctype = "multipart/form-data" })) {
    @Html.AntiForgeryToken()

<div class="form-group">
    <div id="hideLable">
        @Html.Label(Resources.Entity.Product.PdfMessage, new { @class = "text-bold control-label col-md-2" })
    </div>
    <div class="col-lg-6 col-md-8 col-sm-10 ">
        @Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { @class = "form-control tiny-mce", @id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
        @Html.ValidationMessageFor(model => mailModel.PdfMessage)
    </div>
</div>


    <div class="col-xs-12 padding-bottom-10">
        <a href="@Url.Action("GeneratePDFFromHtml", "Product", new { designId = Model.Id })" class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </a>
        @*<button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF</button>*@           
    </div>

}

这也是必须触发的动作方法并创建PDF文件。但该方法未在控制器中触发

问题在于它是局部视图。所以我这样解决了:

Jquery的:

$(document).ready(function () {
        $('#btnGeneratePDF').click(function () {

            var form = document.createElement("form");

            form.setAttribute("GeneratePDFFromHtml", "post");

            form.setAttribute("action", "GeneratePDFFromHtml");

            form.setAttribute("target", "_blank");

            var hiddenField = document.createElement("btnGeneratePDF");

            //hiddenField.setAttribute("name", "search");

            //hiddenField.setAttribute("value", "product1,product2");

            hiddenField.setAttribute("type", "hidden");

            form.appendChild(hiddenField);

            document.body.appendChild(form);

            form.submit();


        });
    });

,视图现在看起来像这样:

<form method="post" id="form" name="form" action="@Url.Action("GeneratePDFFromHtml", "Product")" enctype="multipart/form-data">
        <fieldset>
            <div class="form-group">
                <div id="hideLable">
                    @Html.Label(Resources.Entity.Product.PdfMessage, new { @class = "text-bold control-label col-md-2" })
                </div>
                <div class="col-lg-6 col-md-8 col-sm-10 ">
                    @Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { @class = "form-control tiny-mce", @id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
                    @Html.ValidationMessageFor(model => mailModel.PdfMessage)
                </div>
            </div>

            <div class="col-xs-12 padding-bottom-10">
                @*<a href="@Url.Action("GeneratePDFFromHtml", "Product")" class="btn btn-primary enabled"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF </a>*@
                <button type="submit" value="Create" class="btn btn-success" id="btnGeneratePDF" name="btnGeneratePDF" formtarget="_blank" onclick="GeneratePDFFromHtml()"><i class="fa fa-fw fa-check"></i> @Resources.Action.Navigation.GeneratePDF</button>
            </div>
        </fieldset>
    </form> 

但是按下按钮后,网址就像这样:

http://localhost:51098/Dashboard/Product/Edit/GeneratePDFFromHtml

什么是不正确的。那么如何在控制器中触发该方法?

我也收到此警告:未捕获的ReferenceError:未定义GeneratePDFFromHtml

0 个答案:

没有答案