从视图到控制器未收到Asp Mvc数据 [英] Asp Mvc Data Not Being Received From View To Controller

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

问题描述

Here is complete code   
the values of input box are not being passed to session variables

        public ActionResult Index()
        {
       
            return View("View11" );
        }

 **Index2 Code**
       below
        Here data is being not recieved  
   
  

    public ActionResult Index2()
                {
                    Session["s1"] = Request.Form["My"]; // here values are not being passed
                       Session["s2"] = Request.Form["My2"]; 
                   
                   
                    return View("View2");
                }

      View1 Code 
  html code View1
 

     <form name="form1" action="/Home/index2" method="post">
                    <input  name="My" type="text"  />
                    <input   name="My2" type="text"  />
                    <input id="Submit1" type="submit" value="submit" />
                      </form>
   

    View2 Code--->

        below
      <div> 
    @Session["s1"] 
     @Session["s2"]
    
       </div>

Can someone tell me that why Session Variable is still null

推荐答案

由于你没有使用它的外观任何模型绑定,试试这个:



Since you're not using any model binding by the looks of it, try this:

public ActionResult Index2( FormCollection o){
   ViewData["s1"] = o["My"]; 
   ViewData["s2"] = o["My2"]; 
   return View("View2");
}


您好,



您所关注的流程在这里是错误的。您必须使用HTML帮助程序,如

Hello ,

The process you are following is wrong here. You must use the HTML helpers like the
@Html.BeginForm Or Ajax in order to bind your model.



请查看以下链接,以便更好地了解视图和控制器之间的模型传递。

Html.BeginForm()vs MVC中的Ajax.BeginForm() [ ^ ]

对于Controller和视图之间的数据传递,你可以使用, ViewData,ViewBag或TempData



希望这有帮助,

谢谢


Please check the below link to get a better understanding on the model pass between view and Controller.
Html.BeginForm() vs Ajax.BeginForm() in MVC[^]
For pass of data between Controller and view you can use, ViewData, ViewBag Or TempData

Hope this helps,
Thanks


你好,



我想你刚刚开始你的MVC开发,我假设你很好奇MVC是如何工作的。



解决问题的步骤: -

1.请用H装饰你的Index2动作ttpPost属性。



注意: -

1.附加调试器以查看作为请求对象发送的所有请求。

2.请注意,您没有母版页或任何其他页面,它具有相同名称的Dom元素,您用于两个输入框。(我的,My2)。

3.使用Request.Params [我的]并检查。



虽然这不是最好的方法,但是对于学习和试验MVC这很好。

但实时项目采用MVC模型粘合剂概念。
Hello,

I think you have just started your MVC development , I assume that you are curious about how MVC works .

Steps to solve your problem :-
1. Kindly decorate your Index2 Action with HttpPost attribute .

Note :-
1. Attach Debugger to see what all are the request which are send as request object .
2. Pls see that you dont have masterpage or any other page ,which has same Dom Element with same name u you have used for two input box.(My,My2).
3. Use Request.Params["My"] and check.

Although this is not best approch but for learning and experimenting MVC this is fine .
but in real time project go with MVC Model binders concept.


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

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