从不同视图将变量传递给强类型表单(Pass variable to strongly typed form from different view)

编程入门 行业动态 更新时间:2024-10-26 09:26:07
从不同视图将变量传递给强类型表单(Pass variable to strongly typed form from different view)

我在将url变量传递到单独视图中的表单时遇到了一些麻烦。 基本上我有一个'HomeController' ,它有一个'Contact'动作和视图,一个'SalesController'有一个动作和一个名为'View'的视图

在名为“ View”视图中 (我知道这很混乱),我有一个动作链接,如下所示:

<a href="@Url.Action("Contact", "Home", new { pid = Model.Property[0].Pid, type = "Viewing"})" class="detailsBtn">

如您所见,这是将'pid''type'两个变量传递给'HomeController''Contact'动作。 联系人返回强类型视图,并使用以下模型。

public class ContactModel { [Required] [DataType(DataType.Text)] public string FullName { get; set; } [Required] [DataType(DataType.EmailAddress)] [EmailAddress(ErrorMessage = "Invalid Email Address")] public string Email { get; set; } [Required] [DataType(DataType.PhoneNumber)] [Phone(ErrorMessage = "Invalid Phone Number")] public string Telephone { get; set; } [DataType(DataType.Text)] public string Address { get; set; } [Required] [DataType(DataType.Text)] public string Subject { get; set; } [Required] [DataType(DataType.Text)] public string Message { get; set; } }}

这是联系人视图:

@using (Html.BeginForm()) { <div class="formField"> <span class="label">Full Name *</span><br /> @Html.TextBoxFor(Model => Model.FullName, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.FullName, "Full enter your full name")<br/> </div> <div class="formField"> <span class="label">Email Address *</span><br /> @Html.TextBoxFor(Model => Model.Email, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Email, "Please ensure that a valid email is entered.")<br/> </div> <div class="formField"> <span class="label">Telephone *</span><br /> @Html.TextBoxFor(Model => Model.Telephone, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Telephone, "Please enter a valid telephone number")<br/> </div> <div class="formField"> <span class="label">Property Address (optional)</span><br /> @Html.TextBoxFor(Model => Model.Address, new {@class = "txtContact" })<br /> </div> <div class="formField"> <span class="label">Subject *</span><br /> @if (ViewBag.Pid != null) { @Html.TextBoxFor(Model => Model.Subject, new {@class = "txtContact" }) } else { @Html.TextBoxFor(Model => Model.Subject, new { @class = "txtContact" }) } @Html.ValidationMessageFor(Model => Model.Subject, "Please enter a subject")<br/> </div> <div class="formField"> <span class="label">Message *</span><br /> @Html.TextAreaFor(Model => Model.Message, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Message, "Please enter a message")<br/> </div> <input type="submit" class="contact-btn" value="Send" /> }

我只需要知道最好的方法是从Sales Controller中的Action链接添加传递给Contact Action的'pid'和'type'变量,作为联系表单中我的文本框的值。

I'm having a little trouble passing url variables to a form on a separate view. Basically I have a 'HomeController' which has a 'Contact' action and view, and a 'SalesController' which has an action and a view called 'View'.

On the view called 'View' (confusing I know), I have an action link like so:

<a href="@Url.Action("Contact", "Home", new { pid = Model.Property[0].Pid, type = "Viewing"})" class="detailsBtn">

As you can see, this is passing two variables of 'pid' and 'type' to the 'Contact' action of the 'HomeController'. The contact returns a strongly typed view with the following model being used.

public class ContactModel { [Required] [DataType(DataType.Text)] public string FullName { get; set; } [Required] [DataType(DataType.EmailAddress)] [EmailAddress(ErrorMessage = "Invalid Email Address")] public string Email { get; set; } [Required] [DataType(DataType.PhoneNumber)] [Phone(ErrorMessage = "Invalid Phone Number")] public string Telephone { get; set; } [DataType(DataType.Text)] public string Address { get; set; } [Required] [DataType(DataType.Text)] public string Subject { get; set; } [Required] [DataType(DataType.Text)] public string Message { get; set; } }}

Here's the Contact view:

@using (Html.BeginForm()) { <div class="formField"> <span class="label">Full Name *</span><br /> @Html.TextBoxFor(Model => Model.FullName, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.FullName, "Full enter your full name")<br/> </div> <div class="formField"> <span class="label">Email Address *</span><br /> @Html.TextBoxFor(Model => Model.Email, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Email, "Please ensure that a valid email is entered.")<br/> </div> <div class="formField"> <span class="label">Telephone *</span><br /> @Html.TextBoxFor(Model => Model.Telephone, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Telephone, "Please enter a valid telephone number")<br/> </div> <div class="formField"> <span class="label">Property Address (optional)</span><br /> @Html.TextBoxFor(Model => Model.Address, new {@class = "txtContact" })<br /> </div> <div class="formField"> <span class="label">Subject *</span><br /> @if (ViewBag.Pid != null) { @Html.TextBoxFor(Model => Model.Subject, new {@class = "txtContact" }) } else { @Html.TextBoxFor(Model => Model.Subject, new { @class = "txtContact" }) } @Html.ValidationMessageFor(Model => Model.Subject, "Please enter a subject")<br/> </div> <div class="formField"> <span class="label">Message *</span><br /> @Html.TextAreaFor(Model => Model.Message, new { @class = "txtContact" }) @Html.ValidationMessageFor(Model => Model.Message, "Please enter a message")<br/> </div> <input type="submit" class="contact-btn" value="Send" /> }

I simply need to know the best way for me to add the 'pid' and 'type' variables passed to the Contact Action from the Action link in the Sales Controller as values for my text boxes in the contact form.

最满意答案

您始终可以扩展模型以包含PID和Type属性。 在您的表单中,您可以输入@Html.HiddenFor(Model => model.Pid)和@Html.HiddenFor(Model => model.Type) (或@Html.DisplayFor如果您希望它们可见)

如果您不想这样做,可以将您的PID和Type值从Action中放入ViewBag或ViewData,然后将这些值放入视图中的表单域中。

编辑:如果您希望Pid和Type成为强类型视图的一部分,您可以将模型更改为如下所示:

public class ContactModel { //all your other properties... public string ContactType { get; set; } public string Pid { get; set; } }

然后在您的联系人操作中,您可以:

var model = new ContactModel() { ContactType = type, Pid = pid }; return View(model);

You can always extend your Model to include a PID and Type property. In your form you could just enter @Html.HiddenFor(Model => model.Pid) and @Html.HiddenFor(Model => model.Type) (or @Html.DisplayFor if you want them visible)

If you didn't want to do that, you could put your PID and Type values into ViewBag or ViewData from the Action, and then put these values into form fields in your view.

Edit: If you would like the Pid and Type to be part of your strongly typed view, you can change your model to be like the following:

public class ContactModel { //all your other properties... public string ContactType { get; set; } public string Pid { get; set; } }

Then in your Contact action, you can:

var model = new ContactModel() { ContactType = type, Pid = pid }; return View(model);

更多推荐

本文发布于:2023-07-05 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   表单   变量   类型   Pass

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!