2009-09-02 34 views
1

我在我的控制了以下行動:ASP.NET MVC表單處理問題

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(int id, FormCollection formCollection) 
    { 
     // do stuff with form collection 
    } 

能正常工作。我的問題是,當我使用視圖模型對象(比如MyFormViewModel)沒有屬性包含任何形式的細節。例如有一個名爲MYNAME

一個屬性在我的形式我有名字=「MYNAME」

以上的FormCollection對象包含MYNAME正確的值

,但如果我更改的條目的文本輸入字段上面的代碼爲:

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(int id, String MyName) 
    { 

    } 

然後myName是空的。

有沒有人有任何想法,爲什麼這是這種情況?

編輯:ASPX文件是:

<form action="/mycontroller/edit" method="post" id="myControllerForm" enctype="multipart/form-data"> 
<fieldset> 
<div class="forms"> 
    <div class="row"> 
     <label> Name: </label> 
     <span class="input_wrapper"> 
      <%= Html.TextBox("MyName", Model.MyName, new { @class = "text" }) %> 
     </span> 
    </div> 
    <div class="row"> 
     <input name="" type="submit" /> 
    </div> 
</div> 
</fieldset> 
</form> 
+0

您是否能夠發佈FormCollection類的代碼併發布您的aspx文件。它的刺值看起來有點奇怪。 – user161433 2009-09-03 01:56:01

+0

在上面添加aspx文件 - 道歉的格式 – Yannis 2009-09-03 18:21:34

+0

根據你發佈的代碼確實有效......你能解決這個問題嗎? – aherrick 2009-09-04 03:27:22

回答

1

你有麻煩了使用POST數據更新模式? 如果是這樣,如果你的形式和實際的數據模型,你有字段命名一樣,你可以簡單地做:

// POST: /Room/Edit/5 
[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(int id, FormCollection collection) 
{ 
    // load the actual data model from the DB using whatever ORM you use... 
    MyDataModel model = dataRepository.GetItem(m => m.id == id); 

    try 
    { 
     UpdateModel(model); 
     return View(new MyViewModel(model)); 
    } 
    catch 
    { 
     // error handling... 
     return View(); 
    } 
} 

的的UpdateModel(T O);方法調用將使用Controller的當前ValueProvider中的數據更新提供的對象。

+0

不幸的是,這也不工作。即使在調用UpdateModel(model)之後模型的屬性仍爲空, – Yannis 2009-09-06 09:00:20