post back data to server from partial view and validate model without using javascript or jqueryajx

Scenario is , we have one form tag that is already point to main controller. we have child partial view , that have some text,drop down and submit button. it also have separate controller. when we click on partial view submit button we have to post all data
to that controller (it is possible using ajax in java script we can’t use this thing. because important data in URL is crack by some one else) . so problem is here that we have post data on partial view submit  button and validate model . what to do for that
?

Hi raju,

You can make ajax posts of your forms data. You don’t need to post whole of the data. In your case there must be 2-3 form tags on the page. You can use below ajax method to post data to a particular action method of same controller or some other controller.

//on document.ready call the function
$('#form1').submit(function (evt) { 
//prevent the browsers default function 
evt.preventDefault(); 
//grab the form and wrap it with jQuery 
var $form = $(this); 
//if client side validation fails, don't do anything 
if (!$form.valid()) return; 
//send your ajax request 
$.ajax({ 
type: 'POST', 
url: '/MultiModel/Index', 
data: $form.serialize(), 
dataType: "json", 
traditional: true, 
success: function (response) { 
alert(response); 
} 
}); 
});

But I was tried for submit the partial view it was not working.Thanks

What exactly is happening? Can you please elaborate?

We have One main form and has child form like this

@using(Html.beginform("A","ABC")){

 @Html.Partial("PA", Model.A)

<input type=submit />

}

And Partial View has field like this

@using(html.beginform("PA",B)){

   @Html.TextBoxFor(m => m.abc, new { Class = "form-control", placeholder = "abc" })

<input type=submit/>

}

Here issue is that child partial view submit button point to parent form controller action so i am not able to post back data to server and validate model.

Hi Raju,

You need to move your partial tag out of parent form tag. Currently your submit event is getting triggered via controller "ABC".  DO it something like below.

@using(Html.beginform("A","ABC")){
 
<input type=submit />
 
}

 @Html.Partial("PA", Model.A)

But If I do this I might be able to validate model for partial view.but after validation how can i Post back whole(including data from partial view) data to server for saving in the database ?

Usually forms cannot be nested. if it is mandatory to have you can following work

1. Use one action to handle the both submits from main and partial viewform 2. Name the both submit buttons same 3. capture the submit button text as a parameter in action (button name is parameter, button values is the value in the param) 4. Action should
accpet the both model parameters for the both submissions

and the action looks like this

Lets say button name is ‘btnsubmit’ values are SaveMain and SaveSub

public ActionResult A(string btnsubmit, ModelA modelAObject, ModelPA modelPAObject)
{
    if (btnsubmit == "SaveMain")
    {
        //process the modelAObject
        //return;
    }
    else if (btnsubmit == "SaveSub")
    {
        //process the modelPAObject
        //return;

    }
}

Thanks.

But I have 10 to 12 partial view that loaded into main view and created different different controller to handle particular partial view like usercontrol in C#.net.

If we are using that thing as you say  in main view ,handling all the partial view is very difficult for me.

vraju9896

But I have 10 to 12 partial view that loaded into main view and created different different controller to handle particular partial view like usercontrol in C#.net.

In that case you create one ViewModel class having 10 to 12 properties of type model being used to the partialviews. if they are all same type keep one property as list<model>

and for submitting only the partial views. use input type button instead of submit in partial view and attach jquery ajax call

Hi Raju,

Create one viewmodel and render your strongly typed view with ViewModel. On submit you will get your data in particular entities.

public class ViewModel    {
        public Student MyStudent { get; set; }
        public Teacher MyTeacher { get; set; }
    }

    public class Student
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }

    public class Teacher
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }

 

Leave a Reply