2013-01-10 15 views
1

我不是很有經驗的使用MVC。我正在處理這種情況。一切正常,直到調用其所有成員爲空的HttpPost方法。我不知道爲什麼不堅持所有的數據。如何在回發之間維護對象?

而且一切正常,因爲我可以看到我的Html頁面中的數據,只有當用戶提交信息時纔會發生這種情況。

[HttpGet] 
    public ActionResult DoTest() 
    { 
     Worksheet w = new Worksheet(..); 
     return View(w); 
    } 

    [HttpPost] 
    public ActionResult DoTest(Worksheet worksheet) 
    { 
     return PartialView("_Problems", worksheet); 
    } 

enter image description here

這是一流的,我使用它。

public class Worksheet 
{ 
    public Worksheet() { } 

    public Worksheet(string title, List<Problem> problems) 
    { 
     this.Title = title; 
     this.Problems = problems; 
    } 

    public Worksheet(IEnumerable<Problem> problems, WorksheetMetadata metadata, ProblemRepositoryHistory history) 
    { 
     this.Metadata = metadata; 
     this.Problems = problems.ToList(); 
     this.History = history; 
    } 

    public string Title { get; set; } 
    public List<Problem> Problems { get; set; } // Problem is an abstract class 
    public WorksheetMetadata Metadata { get; set; } 
    public ProblemRepositoryHistory History { get; set; } 
} 

而我的剃刀視圖....剃刀視圖顯示我的看法成功。我意識到一些非常罕見的事情,請注意我的第5行和第6行中有HiddenFor方法,如果我使用了這種方法,那麼當HTTPPOST調用持續存在數據時,我不知道爲什麼。

@model Contoso.ExercisesLibrary.Core.Worksheet 

<div id="problemList"> 
<h2>@Html.DisplayFor(model => model.Metadata.ExerciseName)</h2> 
    @Html.HiddenFor(model => model.Metadata.ExerciseName) 
    @Html.HiddenFor(model => model.Metadata.ObjectiveFullName) 

@for (int i = 0; i < Model.Problems.Count; i++) 
{ 
    <div> 
    @Html.Partial(Contoso.ExercisesLibrary.ExerciseMap.GetProblemView(Model.Problems[i]), Model.Problems[i]) 
    </div> 
} 
</div> 

UPDATE 我使用一個靜態類來獲取視圖名稱,但我測試我只是用這個局部視圖

@model Contoso.ExercisesLibrary.AbsoluteArithmetic.Problem1 
<div> 

    <span style="padding:3px; font-size:18px;">@Model.Number1</span> 
    <span style="padding:5px; font-size:18px;">+</span> 
    <span style="padding:5px; font-size:18px;">@Model.Number2</span> 
    <span style="padding:5px; font-size:18px;">=</span> 

    <span style="font-size:18px"> 
      @Html.EditorFor(model => model.Result, new { style = "width:60px; font-size:18px;" }) 
      @Html.ValidationMessageFor(model => model.Result) 
    </span> 
</div> 

@section Scripts { 

} 

在這裏,用戶在做後期

@model Contoso.ExercisesLibrary.Core.Worksheet 

<form method="post"> 
    @Html.Partial("_Problems", Model) 

    <input type="submit" value="Continue" /> 
</form> 
+4

啊......我以爲ASP.NET MVC的一大優點是:*不再有回傳* ... –

+0

你應該分享你的局部視圖 – rexcfnghk

+0

我已經編輯過你的標題。請參見「[應的問題包括‘標籤’,在他們的頭銜?(http://meta.stackexchange.com/questions/19190/)」,這裏的共識是「不,他們不應該」。 –

回答

2

Model Binder將「捆綁」或鏈接您的視圖模型input領域。它不會綁定顯示字段(如標籤),這就是爲什麼您需要HiddenFor它會添加一個<input type="hidden",然後在您發佈時綁定到模型。

+0

現在我明白了,謝謝..但我想我錯過了一些東西..請注意在帖子中,我有一個部分視圖,其中使用了一個叫做模型Problem1並使用了EditorFor,所以這是一個輸入控件,並且在發佈後不會得到結果。 –

+0

Problem1是一個Problem(抽象類) –

0

確保您的表單標籤類似於以下內容,例如控制器名稱,操作方法,表單方法和表單標識。我指的是@using語句。在我的情況下,控制器名稱是RunLogEntry,操作方法是Create,id是表單。

從View正常投遞到控制器

@using (Html.BeginForm("Create", "RunLogEntry", FormMethod.Post, new { id = "form", enctype = "multipart/form-data" })) 
    { 
     <div id="main"> 
      @Html.Partial("_RunLogEntryPartialView", Model) 
     </div> 
    } 

如果你想通過jQuery後,可以做到以下幾點:

$.post("/RunLogEntry/LogFileConfirmation", 
       $("#form").serialize(), 
       function (data) { 
        //this is the success event 
        //do anything here you like 
                  }, "html"); 
0

您必須指定在正確的屬性窗體您的執行後動作視圖

<form action="Test/DoTest" method="post"> 
... 
</form> 

@using(Html.BeginForm("DoTest", "Test", FormMethod.Post)) { 
... 
} 

第二個建議。

+1

true的實現。但我建議@ Html.BeginForm而不是 – RPM1984

+0

謝謝,我更新了答案 – phnkha

1

您可以使用'TempData'。它用於將數據從當前請求傳遞到後續請求意味着重定向。

此鏈接也可以幫助你。

TempData

SO Tempdata

0

把你的整個HTML代碼下:

@using(Html.BeginForm()) 

標籤。

相關問題