正如评论所说,有很多方法可以实现,关键在于您的需求。
一种方法,如果参数是从page1code behind in the controller传过来的,那么我建议你可以直接把需要传的参数归档到TempData,这样你不需要使用actionlink来传递参数。
public IActionResult Page1()
{
TempData["Area"] = "Products";
TempData["id"] =1;
TempData["ProductName"] = "name";
return View();
}
public IActionResult Create()
{
string Area = TempData["Area"] as string;
int id = (int)TempData["id"];
string ProductName = TempData["ProductName"] as string;
return View();
}
第二种方法,如果允许,可以将这些参数放在form中绑定各个值,如果不想显示这些值,可以设置为@987654327 @类型。
<form asp-action="Create" asp-controller="Product">
<input id="Text1" type="hidden" name="Area" value="Products" />
<input id="Text1" type="hidden" name="id" value="1" />
<input id="Text1" type="hidden" name="ProductName" value="name" />
<input id="Button1" type="submit" value="Create" class="btn btn-primary"/>
</form>
创建动作:
public IActionResult Create(string Area, int id, string ProductName)
{
return View();
}
第三种方法,如果必须将参数从链接传递到另一个视图,则使用ajax结合TempData,请参考以下案例:
public IActionResult Create(string Area, int id, string ProductName)
{
if (Area != null && id != 0 && ProductName != null)
{
TempData["Area"] = Area;
TempData["id"] = id;
TempData["ProductName"] = ProductName;
}
else
{
Area = TempData["Area"] as string;
id = (int)TempData["id"];
ProductName = TempData["ProductName"] as string;
}
return View();
}
查看:
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Scripts{
<script>
$(function () {
$(".mylink").click(function () {
event.preventDefault();
var href = $(this).attr("href").split('?')[0];
var query = $(this).attr("href").split('?')[1];
var arr = query.split("&");
result = {};
for (i = 0; i < arr.length; i++) {
k = arr[i].split('=');
result[k[0]] = (k[1] || '');
};
$.ajax({
method: "Get",
url: href,
data: query,
success: function () {
window.location.href = href;
}
});
});
})
</script>
}
<h1>Index</h1>
@Html.ActionLink("Create", "Create", "Product",
new
{
Area = "Products",
id = 1,
ProductName = "name"
},
new { @class = "btn btn-primary mylink" })
第二种方法的更新:
create action 从其他视图接收到这些值后,您可以使用 model 或 ViewData 在 create view 中显示数据:
型号
public class Test
{
public int id{ get; set; }
public string Area { get; set; }
public string ProductName { get; set; }
}
创建动作:
public IActionResult Create(string Area, int id, string ProductName)
{
Test myTest = new Test()
{
id = id,
Area = Area,
ProductName = ProductName
};
return View(myTest);
}
创建视图:(在视图中显示值)
@model WebApplication_core_mvc.Models.Test
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<table class="table table-bordered">
<tr>
<th>id</th>
<th>Area</th>
<th>ProductName</th>
</tr>
<tr>
<td>@Model.id</td>
<td>@Model.Area</td>
<td>@Model.ProductName</td>
</tr>
查看数据
创建动作:
public IActionResult Create(string Area, int id, string ProductName)
{
ViewData["id"] = id;
ViewData["Area"] = Area;
ViewData["ProductName"] = ProductName;
return View();
}
创建视图:(在视图中显示值)
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<table class="table table-bordered">
<tr>
<th>id</th>
<th>Area</th>
<th>ProductName</th>
</tr>
<tr>
<td>@ViewData["id"]</td>
<td>@ViewData["Area"]</td>
<td>@ViewData["ProductName"]</td>
</tr>
</table>
这是测试结果: