how to get partialview data in controller

2019-05-31 11:58发布

问题:

I am using 3 partialview on a single view, I have a submit button on clicking of which I want to send information to database, I have to retrieve data from all the partialview. Can You please provide me correct information to do it.

Darin I m using L2S so when I drag my stored procedure, I get code some thing like this in

                 [global::System.Data.Linq.Mapping.FunctionAttribute(Name="SP_Name")]
    public int SP_Name(
                [global::System.Data.Linq.Mapping.ParameterAttribute(Name="EmployeeID", DbType="Int")] System.Nullable<int> EmployeeID
{

      IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), EmployeeID);
        encounterID = ((System.Nullable<int>)(result.GetParameterValue(293)));
        return ((int)(result.ReturnValue));
    }
} 

Updated

   <script language="javascript" type="text/javascript">
    $(function () {
        $('#Form1').submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data) {
                    var message = data.Result;
                    $('#Result').html(message);

                }
            });
            return false;
        });
    });

</script>

In my Controller I m using

public ActionResult Index(FormCollection frm)
    {
     My Code ---------------------  
     return Json(new { Result = "Success" });
    }

When I return this I m getting a file in post back and it ask me to save it. I have checked using flidder, in URL it shows me that the path as / only where as If I fill any particular partialview It shows something like /Controller Name/Partialview Can You help me with this problem

回答1:

Well, sending data to a controller action is usually done by performing an HTTP request to this controller action. There are different ways of performing an HTTP request:

  1. Use a <form> tag pointing to this action
  2. Use AJAX

So if you go with the first approach you could have a single <form> wrapping all the partials which would have multiple submit buttons (with different names). Then when you click on one submit buttons all the input fields will be sent to the controller action and then inside the controller action you could process the data based on which submit button was clicked.

If you use the second option, well, then simply harvest the values you need to be sent uipon button click and send them along the AJAX request.


UPDATE:

As requested in the comments section here's how the first technique could be put into action. It uses two partials instead of three but it could be easily extrapolated.

As always you start by defining a view model which will represent the data you would like to work with on this particular view:

public class MyViewModel
{
    public Partial1ViewModel Model1 { get; set; }
    public Partial2ViewModel Model2 { get; set; }
}

public class Partial1ViewModel
{
    public string Foo { get; set; }
}

public class Partial2ViewModel
{
    public string Bar { get; set; }
}

Then a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Model1 = new Partial1ViewModel { Foo = "foo" },
            Model2 = new Partial2ViewModel { Bar = "bar" },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // Here you have access to model.Model1.Foo and model.Model2.Bar =>

        var button = "";
        if (!string.IsNullOrEmpty(Request["submit1"]))
        {
            // submit1 button was used
            button = "submit1";
        } 
        else if (!string.IsNullOrEmpty(Request["submit2"]))
        {
            // submit2 button was used
            button = "submit2";
        }

        var result = string.Format("thanks for submitting using {0}", button);
        return Content(result, "text/plain");
    }
}

and then a main view (~/Views/Home/Index.cshtml):

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Model1)
    @Html.EditorFor(x => x.Model2)
}

and the two corresponding editor templates (or partials if you will):

~/Views/Home/EditorTemplates/Partial1ViewModel.cshtml:

@model Partial1ViewModel
<h2>Partial 1</h2>
<div>
    @Html.LabelFor(x => x.Foo)
    @Html.EditorFor(x => x.Foo)
    <input type="submit" value="Submit me!" name="submit1" />
</div>

~/Views/Home/EditorTemplates/Partial2ViewModel.cshtml:

@model Partial2ViewModel
<h2>Partial 2</h2>
<div>
    @Html.LabelFor(x => x.Bar)
    @Html.EditorFor(x => x.Bar)
    <input type="submit" value="Submit me!" name="submit2" />
</div>