我來自 ASP.NET 背景并且已經開始使用 ASP.NET MVC,所以不確定我在這里做錯了什么還是缺少一些基本的理解。
我有一個PartialView如下所示:
@{
Layout = "master.cshtml";
ContactForm form= new ContactForm();
form.Message = "Thank you, we shall be in touch";
}
我的模型具有該Message屬性,但并未僅在上面的 PV 中設定在我的控制器中。目的是Message對于不同的形式可能是不同的。
我有一種發送電子郵件的方法(這很好并且有效,發送表單中的所有欄位)但我希望Message在form.Message提交表單時將其設定為發送。
我繼續閱讀,ViewBag但我認為這是從控制器到 PV 的溫度值。
代碼運行并設定Message我在除錯時建立的初始加載值。
一旦到達發送電子郵件的方法(單擊按鈕后),除了MessagePV 上設定的值外,所有其他欄位都會加載。
我怎么能從Message區域視圖傳遞給我的控制器?
uj5u.com熱心網友回復:
如果您希望將值發送給控制器,一旦表單提交,您必須為此訊息宣告一個隱藏欄位,記住隱藏欄位必須放在表單內,以便發送給控制器。
@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post))
{
@Html.HiddenFor(model => model.Message)
// Your form content comes here...
}
uj5u.com熱心網友回復:
實際上,如果您像這樣使用 HtmlTagHelper,則可以發送您的模型
@model List<Models.MyModel>
@Html.Partial("MyPartialName",@Model)
絕對你必須像這樣從控制器發送你的模型
public IActionResult Index()
{
.....
....
return View(MyModel);
}
您必須在 Share 檔案夾中創建部分 .cshtml。這個很重要
uj5u.com熱心網友回復:
人.cs
public class Person{
public int Id {get;set;}
public string personName {get;set;}
public string contact {get;set;}
}
MVC控制器
public ActionResult YourMethod(Person){
//Your Operations
}
}
主視圖.cshtml
<form action="YourMethod" method="post">
<input type="text" name = "personName" >
@Html.partial("PartialExample");
<input type="submit" value="submit">
</form>
_PartialExample.cshtml
<input type="text" name="contact" >
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375789.html
標籤:C# asp.net-mvc
