Search code examples
c#razorpartial-viewsajaxformhtml.beginform

How can i hit the controller using Partial View form that have different form type(ajax and begin form)


I have a main form which is rendering a div to a partial view. I loads the controlls very fine. On main form i have a controller which got a function that must get data from the partial view form, when i click the button on partial view,it does not hit the controller(SaveSMRNotesFile).

When i click upload button from the partial view, it goes to SearchUploadNotes controller if i put the break point and that's not the one i want.

How can i pass the data to a controller using partial view?

Main Form

@using (Ajax.BeginForm("SearchUploadNotes", "UploadNotes", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "dvUploadNotes" }, new { @class = "form-horizontal", role = "form" }))
{

<div id="divControlls">
     @{Html.RenderPartial("_UploadNotesControllsGridPartialV", new TTAF.Portal.Parts.Web.Models.UploadNotesFolder.UploadNotesViewModel());}
</div>


<button class="btn btn-primary " id="btnSearch" name="submit" type="submit">Search</button>
 }  

Partial View

@using (Html.BeginForm("SaveSMRNotesFile", "UploadNotes", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" , @id = "form-id", role = "form" }))
{
<button class="btn btn-primary " id="btnn" name="submit" type="sumbit" onclick="">Upload</button>
}

Controller function

 [HttpPost]
 public ActionResult SaveSMRNotesFile(UploadNotesViewModel mymodel)
 {
 }

Solution

  • An approach for your problem could be:

    Main Form

    **@using (Ajax.BeginForm("SearchUploadNotes", "UploadNotes", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "dvUploadNotes" }, new { @class = "form-horizontal", role = "form" }))
    {
    
    @if(ViewBag.Control){
        <div id="divControlls">
             @{Html.RenderPartial("_UploadNotesControllsGridPartialV", new TTAF.Portal.Parts.Web.Models.UploadNotesFolder.UploadNotesViewModel());}
        </div>
        }
    
    <input class="btn btn-primary " id="btnSearch" value="Search" type="submit" formaction="SearchUploadNotes">
     } 
    

    Partial View

    <!-- RENDER YOUR DIFFERENT DATA AFTER YOUR SEARCH -->
    
        <input class="btn btn-primary " id="btnn" value="Upload" type="sumbit" formaction="SaveSMRNotesFile"/>
    

    Controller function

        [HttpPost]
         public ActionResult SaveSMRNotesFile(UploadNotesViewModel mymodel)
         {
    // DO YOUR STUFF COMING FROM THE DATA RECOVERED OF YOUR FORM IN A UploadNotesViewModel OBJECT
         }
        [HttpPost]
         public ActionResult SearchUploadNotes(UploadNotesViewModel mymodel)
         {
        // DO YOUR STUFF COMING FROM THE DATA RECOVERED OF YOUR FORM IN A UploadNotesViewModel OBJECT. RETURNS YOUR DATA AFTER THE SEARCH AND RETURN A VIEWDATA.CONTROL BOOL FOR RENDERING YOUR PARTIAL.
         }
    

    The idea, as far as I got from your post (sorry for my english :)) is:

    You will render just a form with search submit button, if it is clicked, you will render your partial, by ViewData.Control value, with the data searched plus a new submit button for uploading.

    This button call, in the same controller but another method, defined for formaction attribute, called SaveSMRNotesFile, and it will do your stuff. I guess upload the new values for this data.

    Hoping it helps. Let me know any doubt. Cheers mate.