0

Submit Button is not working in FORM page. I have read many solutions for the same question, but not able to rectify the problem. Can somebody please point out the error or suggest change...??

abc.cshtml

     @using (Ajax.BeginForm("ABC", "Queries", null, new AjaxOptions { HttpMethod = "Post",  UpdateTargetId = "content", LoadingElementId = "mask" }, new { enctype = "multipart/form- data", id = "intake_form", **@class** = "inline-form clear_both" }))
    {


     <div class="form_row">
        <div class="form_col1">
            <label class="QueryPageHeadings">
                Title*</label>
        </div>
        <div class="form_col2" style="z-index: 24; position: relative;" id="queryTitle_div">
    @Html.TextBoxFor(m => m.ABC.QueryTitle, new { @class = "text QueryFormTextBox" })
            <br />
            <div style="float: left">
                @Html.ValidationMessageFor(m => m.ABC.QueryTitle)
            </div>
        </div>

    </div>
   <div class="form_row">
        <div class="form_col1">
            <label class="QueryPageHeadings">
                Specific Notes</label>
        </div>
        <div class="form_col2" style="z-index: 21; position: relative;" id="specific_note_div">
            @Html.TextAreaFor(m => m.ABC.SpecificNotes, new { @class = "text QueryFormTextBox", style = "resize:none;height:160px;" })
            <br />
            <div style="float: left">@Html.ValidationMessageFor(m => m.ABC.SpecificNotes)</div>

        </div>
    </div>
    <div class="form_row">
        <div class="form_col1">
            <label class="QueryPageHeadings">
                Supporting Document
            </label>
        </div>
        <div class="form_col2">
            <div style="width:100%;float:left;">
            @Html.TextBoxFor(m => m.ABC.SupportDocument, new { @Id = "lblFileName", @readonly = "readonly" })
            </div>
            <div style="width:100%;float:left;">
            <button id='btnUploadPopup' type="button" style="float:left;" onclick="javascript:OpenFileUpload();">
                Upload Files</button>
                </div>

        </div>

    </div>
    <div class="form_row form_footer">                          
        <p class="indent">
                <input class="secondary button float_right" value="Submit" type="submit" name="button" />                                 
        </p>
    </div>
     }

Controller.cs

    [HttpPost]
    public ActionResult ABC(ABCViewModel model, string button)
    {

            String errorMessage = "";
            if (String.IsNullOrEmpty(errorMessage))
            {
                errorMessage = errorMessage + " Fields can not be empty.";
            }
            if (!String.IsNullOrEmpty(errorMessage))
            {
                Session["errorMessage"] = errorMessage;
                IABC data = new ABCViewModel();

                data.ABC.QueryTitle = model.ABC.QueryTitle;
                data.ABC.SpecificNotes = model.ABC.SpecificNotes;
                data.ABC.SupportDocument = model.ABC.SupportDocument;

                TempData["isEdit"] = false;
                return View("ABC", data);
            }
            else
            {
                Session["errorMessage"] = "";
                Session.Remove("errorMessage");
            }


            Session["fileName"] = null;
            return View();

    }  
2
  • try setting the method to public ActionResult ABC(ABCViewModel model) Nov 12, 2013 at 10:15
  • Show us how you bind your model. I suspect ABCViewModel is only a part of the actual model. Nov 12, 2013 at 11:03

3 Answers 3

0

try to decorate with action method with the httppost attribute

[HttpPost]
public ActionResult ABC(ABCViewModel model, string button)
{
   //your code
}

and change the button type to 'submit'

<button type="submit">....</button>
3
  • 1
    it wont..u need to create two action methods. one without the attribute and return to the View. Then create another ActionResult with the [HttpPost] attribute. try it. Nov 12, 2013 at 10:27
  • I have created two action methods one is Get(without attributes) and second is Post(with Attributes).Above mentioned method is POST. Get is working fine but post is not. When i click on submit button then it should call Post method, but its not. Any suggestions?
    – CodeXerox
    Nov 12, 2013 at 10:42
  • ya GET will work when ur dom is loading. POST will work when u try to post something to the server. plz try to make the button type to 'submit' and giv it a try again. Nov 12, 2013 at 11:25
0

You might be missing the required java scripts since you are doing an AJAX post. Add the following in your _Layout.chtml file.

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Also you need to decorate your controller method with [HttpPost] attribute.

PS: If you do not need asynchronous posing, just use Html.BeginForm instead of Ajax.BeginForm.

3
  • Why you need the button parameter on your action method? Remove it and try. Nov 12, 2013 at 10:55
  • @model Project.ViewModel.ABCViewModel @{ Project.Models.ABCModel ABC = Model.ABC; Project.Models.ABCModel QueryTitle = Model.ABC; Project.Models.RegisterModel registerModel = (Project.Models.RegisterModel)Session["userDetails"]; if (registerModel == null) { registerModel = (Project.Models.RegisterModel)Session["GuestUser"]; } }
    – CodeXerox
    Nov 12, 2013 at 12:35
  • If posible, try changing your binding to Project.Models.ABCModel and return data.ABC from the controller. Nov 12, 2013 at 12:58
0

I've copy-pasted your code "AS-IS" and it works for me. Almost :) (I've changed '**@class**' to '@class' because of compiler error).

Try to repeat it on the empty project. Maybe you have changed something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.