gpt4 book ai didi

c# - 将数据从 View 传递到 Controller (MVC 3)

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:38 24 4
gpt4 key购买 nike

当我尝试将字符串从 View 传递到 Controller 时,我的 ASP.NET 应用程序出现问题...

我的 Controller (需要方法)

`public ActionResult Mail(int id) //Display the View "Mail"
{
Customer customer = db.Customers.Find(id);
return View(customer);
}

[HttpPost,ActionName("Mail")] // this Method allow to send a email
public ActionResult MailConfirmed(int id)
{
Customer customer = db.Customers.Find(id);
string message = Request.Form.Get("messageBody");//Here I try to recuperate my messageBody
if(message!=null)
{
System.Diagnostics.Debug.WriteLine(message);
SendMail(customer.Mail, customer.Name, message);
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("", "Veuillez rentrer un message SVP");
}
return View(customer);
}`

查看“邮件”(经理可以输入邮件正文并点击“邮件”发送邮件的页面)

'@using (Html.BeginForm())
{
<fieldset>
<legend>Customer</legend>

<div class="display-label">Pin</div>
<div class="display-field">
@Html.DisplayFor(model => model.Pin)
</div>

<div class="display-label">Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>

etc...

<div class="display-field">
@Html.TextBox("messageBody",null)//here the value that I try to pass in my controller
</div>


</fieldset>
}
@using (Html.BeginForm())
{
<p>
<input type="submit" value="Mail" />
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</p>
}`

“消息”始终为空...你能帮帮我吗?

最佳答案

像这样改变你的 Controller 方法:

[HttpPost,ActionName("Mail")] // this Method allow to send a email
public ActionResult MailConfirmed(int id, string messageBody)
{
Customer customer = db.Customers.Find(id);

if(messageBody!=null)
{
System.Diagnostics.Debug.WriteLine(messageBody);
SendMail(customer.Mail, customer.Name, messageBody);
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("", "Veuillez rentrer un message SVP");
}
return View(customer);
}

您还从“messageBody”字段传递了 null。将 null 更改为“消息是这样的”,看看它是否有效。

关于c# - 将数据从 View 传递到 Controller (MVC 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com