如何让控制器partialview数据 [英] how to get partialview data in controller

查看:546
本文介绍了如何让控制器partialview数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的单一视图3 partialview,我在点击提交按钮,其中我想将信息发送到数据库中,我必须从所有partialview检索数据。
能否请您为我提供正确的信息来做到这一点。

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.

达林我米使用L2S所以当我拖我的存储过程,我得到code一些像这样的事情在

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));
    }
} 

更新

   <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>

在我的控制器我米用

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

当我回到这个我m到处在后一个文件回来,就问我要保存它。
我一直在使用flidder检查,在URL它显示我的路径/只
在那里,如果我填任何特定partialview它表明类似/控制器名称/ Partialview
你能帮助我解决这个问题。

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

推荐答案

好了,将数据发送到一个控制器动作通常是通过执行一个HTTP请求到该控制器的动作完成。有执行HTTP请求不同的方式:

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. 使用一个&LT;形式为GT; 标记指向这个动作

  2. 使用AJAX

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

所以,如果你用第一种方法去,你可以有一个&LT;形式&GT; 包装所有这些将有多个提交按钮谐音(名称不同)。然后,当你单击一个提交按钮所有输入字段将被发送到控制器动作,然后控制器动作里面,你可以在此基础上提交按钮被点击处理数据。

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.

如果你使用第二个选项,好了,然后简单地收获你需要uipon按钮点击发送和沿AJAX请求给他们的价值观。

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.

更新:

由于在评论部分要求这里的第一种技术可以如何付诸行动。它使用了两个泛音而不是三个,但是它可以很容易地外推。

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.

与往常一样开始通过定义将重新present视图模型,你想在这个特别的看法与工作中的数据:

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; }
}

然后,控制器:

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");
    }
}

然后是主视图(〜/查看/主页/ 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):

〜/查看/主页/ 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>

〜/查看/主页/ 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>

这篇关于如何让控制器partialview数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆