我在将url变量传递到单独视图中的表单时遇到了一些麻烦。基本上我有一个' HomeController' ,它有一个'' 操作和视图,还有一个' SalesController' ,其中包含一个名为' View' 的操作和视图。
在名为' 查看' 的视图中(令我感到困惑),我有一个类似的操作链接:
<a href="@Url.Action("Contact", "Home", new { pid = Model.Property[0].Pid, type = "Viewing"})" class="detailsBtn">
正如您所看到的,这是将&#39; pid&#39; 和&#39; type&#39; 这两个变量传递给 &#39; &#39; HomeController&#39; 的联系&#39; 操作。联系人返回一个强类型视图,其中使用了以下模型。
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" />
}
我只需要知道添加&#39; pid&#39;的最佳方式。和&#39;键入&#39;变量从Sales Controller中的Action链接传递给Contact Action,作为联系表单中文本框的值。
答案 0 :(得分:0)
您始终可以扩展模型以包含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);