2014-10-02 61 views
-1

我在一個頁面中使用兩個html表單,並且必須將我的屏幕值攜帶到第二次提交以顯示過濾器值。但是當我提交第二張表格時,我在我的控制器上使用了空視圖模型。MVC 4將視圖模型攜帶到兩個不同的HTML表單視圖模型未設置

這裏是我的視圖模型

public class MerchantRelationsViewModel 
{ 
    public RelationsSelectCriteria Criteria { get; set; } 
    public MerchantRelationsInfoSelectCriteria CriteriaInfo { get; set; } 
    public MerchantRelations MerchantRelations { get; set; } 

    public IList<MerchantRelationsSequenceRelationsDetailViewModel> SequenceRelationsList { get; set; } 

    public IList<MerchantRelationsMerchantRelationsDetailViewModel> MerchantRelationsList { get; set; } 

    public SelectList MerchantRelationComboList { get; set; } 

    public int selectedItem { get; set; } 
    public MerchantRelationsViewModel() 
    { 
     Criteria = new RelationsSelectCriteria(); 
     CriteriaInfo = new MerchantRelationsInfoSelectCriteria(); 
     MerchantRelations = new MerchantRelations(); 
     MerchantRelationsList = new List<MerchantRelationsMerchantRelationsDetailViewModel>(); 
     SequenceRelationsList = new List<MerchantRelationsSequenceRelationsDetailViewModel>(); 

    } 



    [Required(ErrorMessage = "Merchant no cannot be null.")] 
    [Display(Name = "Merchant No")] 
    public long? MerchantNumber 
    { 
     get 
     { 
      if (Criteria.MerchantNumber == 0) 
       return null; 

      //2. criteria 
      CriteriaInfo.MerchantNumber = Criteria.MerchantNumber; 

      return Criteria.MerchantNumber; 
     } 
     set 
     { 
      Criteria.MerchantNumber = 0; 
      if (value.HasValue) 
       Criteria.MerchantNumber = value.Value; 
     } 
    } 

    [Display(Name = "Merchant Name")] 
    public string SignName 
    { 
     get 
     { 
      return MerchantRelations.SignName; 
     } 
     set 
     { 
      MerchantRelations.SignName = value; 
     } 
    } 

    [Display(Name = "Merchant Type")] 
    public string MainStoreFlagDscr 
    { 
     get 
     { 
      return MerchantRelations.MainStoreFlagDscr; 
     } 
     set 
     { 
      MerchantRelations.MainStoreFlagDscr = value; 
     } 
    } 

    [Display(Name = "Query Type")] 
    public int SelectedMerchantType 
    { 
     get 
     { 
      return CriteriaInfo.Option; 
     } 
     set 
     { 
      CriteriaInfo.Option = value; 
     } 
    } 
} 

下面就我的索引我使用的顯示模板顯示兩個標籤和字段

@using (Html.BeginForm("QueryMerchantRelations", "MerchantRelations", FormMethod.Post, new {})) 
{ 

    <div class="criteria-form criteria-form-three-col"> 
     <div class="clearfix"> 
      <div class="three-column-left"> 
       @Html.DisplayFor(m => m.MerchantNumber, Web.Constants.CompenentNameConstants.DisplayTemplates.NumberOnlyTextBox) 
      </div> 
      <div class="three-column-middle"> 
       <input type="submit" class="nar-btn nar-form-size75" value="Bul" /> 
      </div> 
      <div class="three-column-right"> 
       @Html.DisplayFor(m => m.SignName, Web.Constants.CompenentNameConstants.DisplayTemplates.DisplayTextForTable) 
      </div> 
     </div> 
    </div> 
} 

@using (Html.BeginForm("QueryMerchantRelationsInfo", "MerchantRelations", FormMethod.Post, new { criteriaInfo = Model })) 
{ 
    if (Model.MerchantRelationComboList != null && Model.MerchantRelationComboList.Count() > 0) 
    { 
     <div class="criteria-form criteria-form-three-col"> 
      <div class="clearfix"> 
       <div class="three-column-left"> 
        @Html.DisplayFor(m => m.MainStoreFlagDscr, Web.Constants.CompenentNameConstants.DisplayTemplates.DisplayTextForTable) 
       </div> 
       <div class="three-column-middle"> 
        @Html.DisplayFor(m => m.SelectedMerchantType, Web.Constants.CompenentNameConstants.DisplayTemplates.DisplayDropdown, new { SelectItems = Model.MerchantRelationComboList }) 
       </div> 
       <div class="three-column-right"> 
        <input type="submit" class="nar-btn nar-form-size75" value="Göster" /> 
       </div> 
      </div> 
     </div> 
    } 

我的控制器

[HttpPost] 
    public virtual ActionResult QueryMerchantRelations(MerchantRelationsViewModel merchantRelationsViewModel) 
    { 
     //Call First service and set into view model for second criteria search 


     return View("Index", merchantRelationsViewModel); 
    } 

    [HttpPost] 
    public virtual ActionResult QueryMerchantRelationsInfo(MerchantRelationsViewModel merchantRelationsViewModel) 
    { 
     //Call second service for retrieving second data 

     return View("Index", merchantRelationsViewModel); 
    } 

當我將QueryMerchantRelationsInfo稱爲MerchantRelationsViewModel爲空。我試圖給HTML.BeginForm參數中的模型,但沒有改變。我試圖使用隱藏的,我不能給那裏整個模型。有沒有辦法做到這一點沒有j查詢ajax調用?

回答

-1

您應該使用PartialViews和Ajax。讓你的控制器返回PartialView。並在視圖中渲染部分視圖並使用Ajax發佈表單。

0

我也不太清楚什麼是你想完成但是,更新您的視圖

<div class="criteria-form criteria-form-three-col"> 
    <div class="clearfix"> 
     <div class="three-column-left"> 
      @Html.DisplayFor(m => Model.MerchantNumber) 
      @Html.HiddenFor(m=>Model.MerchantNumber) 
     </div> 
     <div class="three-column-middle"> 
      <input type="submit" class="nar-btn nar-form-size75" value="Bul" /> 
     </div> 
     <div class="three-column-right"> 
      @Html.DisplayFor(m => Model.SignName) 
      @Html.HiddenFor(m=>Model.SignName) 
     </div> 
    </div> 
</div> 
    @using (Html.BeginForm("QueryMerchantRelationsInfo", "Home", FormMethod.Post, new { criteriaInfo = Model })) 
{ 
@Html.HiddenFor(m => Model.MerchantNumber) 
@Html.HiddenFor(m => Model.SelectedMerchantType) 
if (Model.MerchantRelationComboList != null && Model.MerchantRelationComboList.Count() > 0) 
{ 
<div class="criteria-form criteria-form-three-col"> 
    <div class="clearfix"> 
     <div class="three-column-left"> 
      @Html.DisplayFor(m => m.MainStoreFlagDscr) 
     </div> 
     <div class="three-column-middle"> 
      @Html.DisplayFor(m => m.SelectedMerchantType, new { SelectItems =   Model.MerchantRelationComboList }) 
     </div> 
     <div class="three-column-right"> 
      <input type="submit" class="nar-btn nar-form-size75" value="Göster" /> 
     </div> 
    </div> 
</div> 
    } 
} 
+0

我們不希望從第一服務鬆檢索到的數據。 – arettatonyan 2014-10-03 07:54:44