Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅在HttpPost ActionHandler之后呈现部分视图_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 仅在HttpPost ActionHandler之后呈现部分视图

C# 仅在HttpPost ActionHandler之后呈现部分视图,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在使用HttpPost ActionHandler中的MyViewRequest视图字段进行WCF服务调用。目标是使用部分视图显示响应,myviewsresponse 简言之,我需要实现这两项目标- 在第一次加载时禁用部分视图的加载 服务呼叫后显示响应(连同请求) MyViewRequest.cshtml @using (Html.BeginForm()) { @Html.ValidationSummary(false) //html code } </div>

我正在使用HttpPost ActionHandler中的MyViewRequest视图字段进行WCF服务调用。目标是使用部分视图显示响应,
myviewsresponse

简言之,我需要实现这两项目标-

  • 在第一次加载时禁用部分视图的加载
  • 服务呼叫后显示响应(连同请求)
  • MyViewRequest.cshtml

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)
        //html code
    }
    </div>
    <div id="dvResponse">
      @Html.Partial("MyViewResponse");
    </div>
    
    @model MvcApplication3.Models.MyModel
    @{
        ViewBag.Title = "MyViewResponse";
    }
    
    <h2>MyView</h2>
    
    @Html.Label(Model.MyName, "My Name")
    
    @使用(Html.BeginForm())
    {
    @Html.ValidationSummary(false)
    //html代码
    }
    @Html.Partial(“MyViewResponse”);
    
    部分视图:MyViewResponse.cshtml

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)
        //html code
    }
    </div>
    <div id="dvResponse">
      @Html.Partial("MyViewResponse");
    </div>
    
    @model MvcApplication3.Models.MyModel
    @{
        ViewBag.Title = "MyViewResponse";
    }
    
    <h2>MyView</h2>
    
    @Html.Label(Model.MyName, "My Name")
    
    @model mvcapapplication3.Models.MyModel
    @{
    ViewBag.Title=“MyViewResponse”;
    }
    MyView
    @Html.Label(Model.MyName,“我的名字”)
    

    这在Asp.Net中使用userControl非常简单,但是在这里,我们如何在MVC3中实现这一点呢。

    我认为最好的方法是使用ViewModels传输数据。假设你想要一个类似stackoverflow的应用程序,其中你有一个问题,用户可以发布一个答案,它将在发布后与问题一起显示

    public class PostViewModel
    {
      public int ID { set;get;}
      public string Text { set;get;}
      public List<PostViewModel> Answers { set;get;}
      public string NewAnswer { set;get;}
    }
    
    假设
    yourService.GetQuestionFromID
    方法返回一个填充了属性值的PostViewModel对象。可以从数据库或通过WCF服务调用获取数据。这取决于你。另外,
    yourService.GetAnswersFromQuestionID
    方法返回PostViewModel列表,以表示该问题的答案。您可以将这两种方法放在一个名为
    GetQuestionWithAnswers
    的方法中。我写了两个方法来让它更清晰

    现在在你的节目视图中

    @model PostViewModel
    @Html.LabelFor(x=>x.Text);
    @using(Html.Beginform())
    {      
      @Html.HiddenFor(x=>x.ID);     
      @Html.TextBoxFor(x=>x.NewAnswer)
      <input type="submit" />
    }
    <h3>Answers</h3>
    @if(Model.Answers!=null)
    {
      @Html.Partial("Responses",Model.Answers)
    }
    
    处理回发很简单(HttpPost)


    您希望在什么情况下进行WCF调用并显示响应?@Shyju我在
    HttpPost
    ActionHandler中进行服务调用,当前显示默认视图。i、 e.
    返回视图()
    谢谢。得到1个问题,正如建议的那样,我创建了一个
    ViewModel
    ,其中包含
    Request
    Response
    。在回发时,我填充了ViewModel的
    Response
    对象,并再次呈现了相同的视图。空检查的计算结果为true(
    Model.Answers!=Null
    )。我的请求模型包含了一个
    DropDownDist
    ,回发后我希望它的值被重置。但用户选择仍然存在。。。我认为MVC本质上是无状态的。在这种情况下,状态是如何保持的?回发后,我没有重新填充
    Request
    对象的
    ViewModel
    。@autrevo我猜您正在将发布的模型传递给View方法。
    [HttpPost]
    public ActionResult Show(PostViewModel model)
    {
      if(ModelState.IsValid)
      {
        //get your data from model.NewAnswer property and save to your data base 
        //or call WCF method to save it.
        //After saving, Let's redirect to the GET action (PRG pattern)
        return RedirectToAction("Show",new { @id=model.ID});
      } 
    }