2017-05-08 53 views
0

我无法弄清楚我做错了什么。 我的项目启动了一个索引视图,发布到fileUpload操作。 从fileUpload动作中,控制器中的另一个方法称为changeText(),在changeText()结尾处调用带有参数字符串的Results(文本)。在Results()的最后,有一行返回View()到Results.cshtml。但Results.cshtml不会被加载。 只是想知道我是否错过了一些东西。从一个单独的控制器加载视图在.net中的动作

 public ActionResult Index() 
     { 

      return View(); 
     } 

     [HttpPost] 
     public void FileUpload(HttpPostedFileBase file) 
     { 
      Debug.WriteLine(file.FileName) 
      if (file != null) 
      { 
       var fileName = Path.GetFileName(file.FileName); 
       pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
       file.SaveAs(pathName); 
      } 

      changeText(txt); 

     } 


    public void changeText (string text) 
    { 

       ResultX(textChange);    
    } 



     public ActionResult ResultX(string text) 
     { 

       Debug.WriteLine("resultx action"); 
       return View(text); 

     } 

Thanks. 
+0

份额控制器的右视图。 – OmG

+0

请更新您正在使用的正确版本的asp.net MVC的标签。 – ps2goat

回答

1

我看不出所有这些方法的原因!你的代码可以非常简单。

public ActionResult Index() 
    { 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult FileUpload(HttpPostedFileBase file) 
    { 
     Debug.WriteLine(file.FileName) 
     if (file != null) 
     { 
      var fileName = Path.GetFileName(file.FileName); 
      pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
      file.SaveAs(pathName); 
     } 

     Debug.WriteLine("resultx action"); 
     return View("ResultX",text); 

    } 

如果这一切不工作确保你打电话

2

这是你的代码应该是什么:

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

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase file) 
{ 
    Debug.WriteLine(file.FileName) 
    if (file != null) 
    { 
     var fileName = Path.GetFileName(file.FileName); 
     pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
     file.SaveAs(pathName); 
    } 

    return changeText(txt); 
} 

public ActionResult changeText (string text) 
{ 
     return ResultX(textChange);    
} 

public ActionResult ResultX(string text) 
{ 
     return View("ResultX", text); 
} 
1

它似乎并不像你真正明白行为是如何工作的。首先,动作的返回类型决定了发送给客户端的响应。返回void意味着您只需返回一个带有200状态码的空响应主体。就通过网络浏览器访问您的网站的用户而言,这意味着一个完全空白的页面,这不是您想要的。

你想在这里,返回ViewResult利用ResultX.cshtml。这意味着您的FileUpload操作需要返回ViewResult或更一般地ActionResultActionResult是所有其他的结果类型的基类,因此,如果您键入您的回报方式,你可以返回任何样的结果你喜欢,不管是ViewResultRedirectResultEmptyResult

鉴于此,存在因为方法是多余的,所以changeTextResultX。没有理由在三种不同的方法之间传递这些信息,这几乎都是同样的事情。请直接在FileUpload中进行return View("ResultX", txt);。但是,一个成功的帖子应该遵循PRG(Post-Redirect-Get)模式,所以如果用户刷新页面,表单不会被重新提交。因此,你应该真的成功返回重定向,并返回错误的原始FileUpload视图。总之,这看起来像:

[HttpPost] 
public ActionResult FileUpload(HttpPostedFileBase file) 
{ 
    if (file != null) 
    { 
     // Moved this inside the conditional because you can't 
     // reference `file.FileName` unless `file` is non-null 
     Debug.WriteLine(file.FileName); 

     var fileName = Path.GetFileName(file.FileName); 
     pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName); 
     file.SaveAs(pathName); 
     return RedirectToAction("Index"); 
    } 

    // Traditionally, you'd pass in what was posted to the call to `View`, 
    // but you cannot repopulate a file input, so there's no point here. 
    return View(); 
} 
相关问题