.submit()阻止表单提交

时间:2018-05-29 03:09:19

标签: c# jquery asp.net-mvc forms

我有一个相当简单的表单和ViewModel工作正常,但是当我添加这个JS时,表单不再提交给控制器:

$("#crmtForm").submit(function (e) {
    console.log('submit');
});

为什么呢?我很确定这应该有用......请有人帮忙,这没有任何意义。

控制器

[HttpPost]
public async Task<ActionResult> Create(CRMTItemViewModel viewModel)
{
    viewModel.CreatedBy = System.Security.Claims.ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == "name").Value;
    if (viewModel.ProjectTitle == "spinnertest")
        return View();

    // Insert db rows?
    var crmtItem = await crmtItemsManager.InsertItem(viewModel);

    // Initialise workspace on a seperate thread
    new Thread(() =>
    {
        var projectManager = new ProjectManager();
        projectManager.ProcessRequest(crmtItem);
    }).Start();

    // Redirect to item
    return RedirectToAction("Details", new { id = crmtItem.Id });
}

表格

@using (Html.BeginForm("Create", "CrmtItems", FormMethod.Post, new { id = "crmtForm" }))
{
<div class="form-horizontal">
    <h4>New Project Workspace Form</h4>
    <hr />


    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.ProjectTitle, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProjectTitle, new { htmlAttributes = new { @class = "form-control", required = "required" } })
            @Html.ValidationMessageFor(model => model.ProjectTitle, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProjectStage, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.ProjectStage, new { @class = "form-control", required = "required" })
            @Html.ValidationMessageFor(model => model.ProjectStage, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CRMTNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CRMTNumber, new { htmlAttributes = new { @class = "form-control", required = "required" } })
            @Html.ValidationMessageFor(model => model.CRMTNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.GbSNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.GbSNumber, new { htmlAttributes = new { @class = "form-control", required = "required" } })
            @Html.ValidationMessageFor(model => model.GbSNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Confidential, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.Confidential, new { @class = "form-control", @style = "height:17px;" })
            @Html.ValidationMessageFor(model => model.Confidential, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => Model.SelectedTags, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.HiddenFor(m => m.Id, new { required = "required" })
            @Html.ListBoxFor(m => m.SelectedTags, new SelectList(users, "UserName", "DisplayName"), new { @class = "teamSelecter", name = "states[]", multiple = "multiple", style = "display:none; width:100%;", required = "required" })
            @Html.ValidationMessageFor(model => model.SelectedTags, "", new { @class = "text-danger" })
            <p id="pmWarning" class="text-danger" hidden>Please select one or more project managers</p>
        </div>
    </div>

    <br />
    <button id="formSubmit" class="btn btn-default btn-lg pull-right" type="submit" value="submit">Submit</button>
    <div class="loader pull-right" hidden></div>

</div>
}

视图模型

public class CRMTItemViewModel
{
    public int Id { get; set; }

    [Display(Name = "Project Title")]
    [Remote("DoesProjectTitleExist", "CRMTItems", HttpMethod = "POST", 
        ErrorMessage = "Workspace for that project title already exists.")]
    public string ProjectTitle { get; set; }

    [Display(Name = "Project Stage")]
    public ProjectStage? ProjectStage { get; set; }

    [Display(Name = "CRMT Number")]
    [Remote("DoesCrmtNumberExist", "CRMTItems", HttpMethod = "POST",
        ErrorMessage = "Workspace for that CRMT number already exists.")]
    public int? CRMTNumber { get; set; }

    [Display(Name = "GBS Number")]
    [Remote("DoesGbSNumberExist", "CRMTItems", HttpMethod = "POST", 
        ErrorMessage = "Workspace for that GBS project number already exists.")]
    public int? GbSNumber { get; set; }

    public bool Confidential { get; set; }

    [Display(Name = "Project Managers")]
    public IEnumerable<string> SelectedTags { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我在我的项目中尝试了你的代码工作,可能是你的jquery版本的问题,我在创建脚手架视图时发现的另一件事是这个模型它在视图页面的末尾自动创建@section Scripts { @Scripts.Render("~/bundles/jqueryval") }, 当我从页面中删除此脚本,然后能够调用post方法,否则没有。