将字符串传递给action方法重载

时间:2015-06-17 09:03:31

标签: html asp.net-mvc

所以我想做的是从textarea获取一个字符串并将其传递给action方法重载(string paramJSON)。

行动方法:

public ActionResult SendMail(string templateName, string receiver, string paramJSON)
{
    var paramDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(paramJSON);
    new SendMailClient().Send(templateName, receiver, paramDictionary);

    if(Request.IsAjaxRequest())
    { 
        return RedirectToAction("SendPartial", "TestMail");
    }
        return View();
}

textarea的:

@Html.TextAreaFor(a => a.TestParametrar, new { id = "paramTxt" })

2 个答案:

答案 0 :(得分:1)

你的html助手

@Html.TextAreaFor(a => a.TestParametrar, new { id = "paramTxt" })

生成一个带name="TestParametrar"的textarea。当您提交表单时,它会发送回每个控件namevalue属性的值TestParametrar: 'The text you entered'您的方法需要包含一个具有相同名称的参数,例如

public ActionResult SendMail(string TestParametrar, ....)

,参数的值将是表单控件中输入的文本。

但是,由于您查看的是基于模型,因此最好只发回模型并绑定所有属性

 public ActionResult SendMail(YourModel model)

具有验证属性的额外好处。例如,如果属性TestParametrar具有[Required]属性,那么如果用户未输入值,则ModelSTate将无效,并且可以返回视图以进行更正。

答案 1 :(得分:0)

@using (Html.BeginForm("SendMail2"))
{
   @Html.TextAreaFor(a => a.TestParametrar, new { id = "paramTxt" })
   <input type="submit" value="Send Message" />
}

public ActionResult SendMail2(string TestParametrar)
{
    return SendMail("myTemplate", "hello@world.com", TestParametrar);
}
相关问题