2017-06-14 62 views
0

我想說清楚,我是MVC的超級新手,並且從來沒有做過任何接近web開發的事情,所以請記住。C#mvc控制器到類

我的問題與我的標題密切相關,但首先這是我想要做的。

我有一個textarea,用戶鍵入一些數據,然後單擊操作按鈕。該文本然後發送到我的控制器,我希望我的控制器然後將該值傳遞給我的類功能。該函數將執行該操作,然後將值返回給我的控制器,然後再重新更新textarea。

到目前爲止,我只有textarea將值傳遞給我的控制器。什麼我的問題是,我怎麼能那麼有我的控制器跟我的班,然後再值回到我的textarea ...

public class TTHController : Controller 
    { 
    // GET: Text_To_Hex 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult MyAction(string comment) 
    { 
     [Call calss here](comment) 
     return ""; 
    } 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

@using (Html.BeginForm("MyAction", "TTH")) 
{ 
    @Html.TextArea("comment") 

    <input type="submit" value="Submit" /> 
} 
+1

您似乎錯過了項目的「模型」元素。 – Wheels73

+0

'var obj = new myClass(); var retValue = obj.Method(comment);'這是你在控制器的Post Action方法中所需要的。用您的班級名稱和'method'替換您的班級中的'MyClass'。 –

回答

1

下面應該達到你所需要的。

創建一個模型來保存評論

public class MyViewModel 
{ 
    public string Comments {get;set;} 
} 

public class TTHController : Controller 
    { 
    // GET: Text_To_Hex 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult MyAction(MyViewModel model) 
    { 
     //Do class stuff 
     //The comments will already be bound to the model in your post. 
     //Just redirect passing in the same model. 

     return RedirectToAction("Index", model); 
    } 

使用TextAreaFor綁定到的意見模型屬性

@model MyViewModel; 

@{ 
    ViewBag.Title = "Index"; 
    } 

<h2>Index</h2> 

@using (Html.BeginForm("MyAction", "TTH")) 
{ 
    @Html.TextAreaFor(x=> x.Comments); 

    <input type="submit" value="Submit" /> 
} 

希望指向你在正確的方向。

+0

它對此表示感謝。 –