2012-05-25 41 views
0

使用Fiddler我可以看到該請求甚至沒有被創建,但我看不出爲什麼。ASP.NET MVC 3(Razor)表單提交不起作用

這裏的形式:

@using (Html.BeginForm("Index", "FileSystemChannelIndex", FormMethod.Post, new { 
channelId = @Model.ChannelId })) 
{ 
    @Html.HiddenFor(model => model.Id) 
    @Html.HiddenFor(model => model.ChannelId) 
    <div class="editor-label"> 
     Select File Source 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(
      model => model.SelectedFileSourceValue, 
      new SelectList(Model.AvailableFilesSources, "Id", "Name"), 
      new { id = "selectFileSource" }) 
    </div> 
    <p> 
     <input class="t-button" type="submit" value="Save" /> 
    </p> 
} 

觀最初來自:

public ViewResult Create(int channelId) 
{ 
    var channel = this.fullUOW.GetFileSystemChannelRepository().All.Where(c => c.Id == channelId); 
    var vm = new FileSystemChannelIndexViewModel(channelId, new FileSystemChannelIndex()); 
    return View("Edit", vm); 
} 

我試着加入「名」屬性的,但沒有任何區別。

任何想法?

編輯:吉姆等更多信息...

域:

public class FileSystemChannel 
{ 
    public int Id {get; set; } 
    public ICollection<FileSystemChannelIndex> ChannelIndexes { get; set; } 
} 

public class FileSystemChannelIndex 
{ 
    public int Id { get; set; } 
    public FileSystemChannel ParentChannel { get; set; } 
} 

由於0 ... *協會,在UI,我們必須先創建一個FileSystemChannel然後爲它添加一個FileSystemChannelIndex。所以這就是爲什麼我將channelId傳遞給FileSystemChannelIndex創建視圖。在提交新FileSystemChannelIndex以下行動應該叫:

[HttpPost] 
public ActionResult Index(int channelId, FileSystemChannelIndexViewModel vm) 
{ 
    //TODO: get the Channel, add the Index, save to db 

    return View("Index"); 
} 
+0

你有一個ActionResult調用帶有[[HttpPost]數據註釋的索引嗎? –

+0

你必須得到一些錯誤?那些錯誤是什麼? – Rafay

+0

@Tim Yep,雖然我不確定簽名是否正確。 – empo

回答

3

所以感謝馬克的評論,這是由於選擇失敗的客戶端驗證。使用IE開發工具來檢查元素:

<select name="SelectedFileSourceValue" class="input-validation-error" id="selectFileSource" data-val-required="The SelectedFileSourceValue field is required." data-val-number="The field SelectedFileSourceValue must be a number." data-val="true"> 
1

EMPO,

進一步上面我的評論:

EMPO - 你可以發佈兩種public ActionResult Create(////)方法(即HttpPostHttpGet)進入問題,因爲這可能會突出顯示問題是否與模糊的方法簽名有關,我懷疑當您發回與HttpGet操作結果相同的簽名時可能會發生這種情況

嘗試加入適當的HttpPost的ActionResult線沿線的:

[HttpPost] 
public ActionResult Create(FileSystemChannelIndex domainModel) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(PopulateEditViewModel(domainModel)); 
    } 

    _serviceTasks.Insert(domainModel); 
    _serviceTasks.SaveChanges(); 
    return this.RedirectToAction("Edit", new {id = domainModel.ChannelId}); 
} 

原來的HTTPGET(這感覺 '奇怪' 給我):

[HttpGet] 
public ViewResult Create(int channelId) { 
    var channel = this.fullUOW.GetFileSystemChannelRepository().All 
     .Where(c => c.Id == channelId); 
    var vm = new FileSystemChannelIndexViewModel(channelId, 
     new FileSystemChannelIndex()); 
    return View("Edit", vm); 
} 

和你編輯的ActionResult裏面,你」 d根據傳入的id獲取實體。可能有效,可能不會。不確定沒有您的域和邏輯的更全面的圖片。

很明顯,你自己的管道會有所不同,但是這應該給出一個應該預期的東西的概念。

+0

Jim,看看我上面的編輯。我不認爲這是由於控制器,因爲該網頁甚至沒有提出請求。 – empo

+0

Jim沒有[HttpPost] Create,因爲我想返回到Index視圖,就像你在Html.BeginForm參數中看到的那樣。我開始認爲我得到了這一切都錯了,應該有一個被稱爲創建操作,但返回索引視圖 – empo

+2

empo-我認爲如果使用索引作爲一個httppost操作可能會錯誤。標準的做法是有一對「行動」,一個取得所需的對象和值的帖子,另一個接受POST的值,然後做一個重定向(PRG)。 –

0

當你創建一些東西時,你如何能夠使用Model.Id?也許Model.Id爲空,因爲你不能發佈

+0

這是創建和編輯的相同視圖。如果id = 0,那麼創建if id> 0然後編輯 – empo

+0

爲什麼你需要這個? new { channelId = @ Model.ChannelId} – karaxuna

+0

channelId隱藏字段將被張貼 – karaxuna