将数据从视图传递到控制器方法 [英] Pass data from view to controller method

查看:177
本文介绍了将数据从视图传递到控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控制器的方法中使用当前视图的名称 我可以通过以下代码获取名称.

I need to use the name of the current view in a method in my controller I am able to get the name with the code below in my view.

我想将此@ViewData ["pageName"]传递给我的MakeChange操作结果 在我的控制器中.每次我执行MakeChange方法时,我都会得到 是对象引用未设置为对象的实例"

I will like to pass this @ViewData["pageName"] to my MakeChange action result in my controller. Each time I step through the MakeChange method all I get is "object reference not set to an instance of an object"

如何将数据从视图传递到控制器方法?

How can I pass data from my view to controller method ?

            @ViewData["pageName"] =  @Path.GetFileName(Server.MapPath(VirtualPath))


             public ActionResult MakeChange(string lang)
                    {
                        string getPageName = ViewData["pageName"].ToString();

                        return RedirectToAction(getPageName, "Home");
                    }

推荐答案

Path.GetFileName(Server.MapPath(VirtualPath))将为您提供带扩展名的剃刀视图名称(Ex:index.cshtml).您不能将其与RedirectToAction方法一起使用,因为RedirectToAction方法正在寻找操作方法名称.您需要先缩小文件扩展名部分.

Path.GetFileName(Server.MapPath(VirtualPath)) will give you the razor view name with extension (Ex : index.cshtml). You can not use that with RedirectToAction method as RedirectToAction method is looking for an action method name. You need to trim down the file extension part before using it.

要将其发送到控制器操作,可以将值保留在表单内的隐藏字段内.当用户发布表单时,它将在您的HttpPost操作方法中可用.您需要确保有一个与隐藏字段的名称值具有相同名称的参数.

To send this to the controller action, you can keep the value inside a hidden field inside your form. When user posts the form, it will be available in your HttpPost action method. You need to make sure that there is a parameter which has same name as the hidden field's name value.

@using (Html.BeginForm())
{
   <input type="text" name="lang" value="English" />
   <input type="hidden" name="pageName" 
                                 value="@Path.GetFileName(Server.MapPath(VirtualPath))" />

   <input id="BtnAdd" type="submit" value="Save" />

}

所以您的操作方法将会是

So your action method will be

public ActionResult MakeChange(string lang,string pageName)
{
   var viewName=pageName;
   //Get rid of the extension.
   viewName = viewName.Replace(".cshtml","");
   return RedirectToAction(viewName , "Home");
}

即使您正在做ajax发布,它仍然可以工作,只需序列化表格并发送

Even if you are doing an ajax post, it will still work, just serialize your form and send it

$("#BtnAdd").click(function(e){
   e.preventDefault();
   var _this = $(this); 
   $.post("@Url.Action("MakeChange","Home")",_this.closest("form").serialize(),
                                                                       function(response){

        //do something with response
    });
});

有很多更干净的方法来获取视图名称,而无需执行文件扩展名调整方法.看看此答案.

There are more clean ways of getting the view name without the file extension trimming approach we did. Take a look at this answer.

这篇关于将数据从视图传递到控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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