在MVC中没有得到控制器的值 [英] Value not getting post to controller in MVC

查看:88
本文介绍了在MVC中没有得到控制器的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个局部视图。我在复选框点击事件的ajax调用上呈现该视图。用户将在局部视图的文本框中输入值,然后单击传递按钮。问题是,有时我能够在控制器上获取文本框值有时不会。我真的无法理解出了什么问题。我在视图页面上使用简单的html.BeginForm方法将所有数据从视图发布到控制器。



这是控制器操作:

Hi,

I have one partial view. I am rendering that view on ajax call of checkbox click event. User will enter the values in partial view's textbox and will click on passed button. Issue is that sometime I am able to get the textbox value on controller sometimes not. I am really not able to understand what is going wrong. I am using simple html.BeginForm method on view page to post all the data from view to controller.

This is the controller action:

[HttpPost] 
public ActionResult SetTransaction(UserBOM) { 
  if (
       (UserBOM.CompletedResponse != null && ReasonName == "Contacted")
       || UserBOM.IdEnable == true
  ) {
    TempData["IdEnable"] = UserBOM.IdEnable;
    TempData["ApplicantId"] = UserBOM.ApplicantId;    // intermittent not getting value.
    TempData["ApplicantNumber"] = UserBOM.ApplicantNumber;
    TempData.Keep("IdEnable"); 
    TempData.Keep("ApplicantId");
    TempData.Keep("ApplicantNumber"); 
  }
}



视图


And the view

@using (Html.BeginForm("SetTransaction", "BranchUser", FormMethod.Post)) {
  //  partial view rendered through ajax call
}



我看到的问题是在SetTransaction(UserBOM)方法中,userBOM变为null。有时它按预期工作但不总是。原因可能是什么。谢谢。



我尝试了什么:



我无法每次都重现它作为其间歇性问题而且真的很难解决这个问题。


The problem that I am seeing is "In SetTransaction(UserBOM) method, userBOM is getting null. Sometimes it is working as expected but not always. What could be the reason. Thanks."

What I have tried:

I am not able to reproduce it every time as its intermittent issue and really finding difficult to fix this issue.

推荐答案

引用:

真的很难解决这个问题。

really finding difficult to fix this issue.





如果您没有提供发送数据的方式,我们会更难帮助您。如何填充 UserBom 对象?在开始使用MVC Action方法引用 UserBom 对象之前,您应该先开始查看该代码。



另外,在引用对象的属性之前,请务必检查 null ,以避免意外错误。



It's much more harder for us to help you if you didn't provide how you are sending the data. How do you populate your UserBom object? You should start looking at that code first before going to your MVC Action method to reference the UserBom object.

On a side note, always check for null before referencing a property from your object to avoid unexpected errors.


这篇关于在MVC中没有得到控制器的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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