Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC通过ActionLink传递模型_Asp.net_Asp.net Mvc - Fatal编程技术网

ASP.NET MVC通过ActionLink传递模型

ASP.NET MVC通过ActionLink传递模型,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我想在单击ActionLink时将模型和int传递给控制器 @Html.ActionLink("Next", "Lookup", "User", new { m = Model.UserLookupViewModel, page = Model.UserLookupViewModel.curPage }, null) 不起作用,而是传递一个模型的空白实例,这是使用new时所期望的 @Html.ActionLink("Next", "Lookup", "User", Model.UserLook

我想在单击
ActionLink
时将模型和
int
传递给控制器

@Html.ActionLink("Next", "Lookup", "User", new { m = Model.UserLookupViewModel, page = Model.UserLookupViewModel.curPage }, null)
不起作用,而是传递一个模型的空白实例,这是使用
new
时所期望的

@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
确实有效

控制器

[HttpGet]
public ActionResult Lookup(UserLookupViewModel m, int page = 0)
{
    return this.DoLookup(m, page);
}
查看模型

public class UserLookupViewModel
{
    public int curPage { get; set; }

    [Display(Name = "Forenames")]
    public string Forenames { get; set; }

    [Display(Name = "Surname")]
    public string Surname { get; set; }

    [Display(Name = "DOB")]
    public DateTime? DoB { get; set; }

    [Display(Name = "Post code")]
    public string Postcode { get; set; }
}

我怎样才能把它们一起传递?
Lookup
方法的参数与ActionLink中的命名属性匹配。

不能使用ActionLink传递带有链接的属性,但可以执行以下操作以获得相同的行为

<form action="/url/to/action" Method="GET">
  <input type="hidden" name="Property" value="hello,world" />
  <button type="submit">Go To User</button>
</form>
是因为ActionLink方法的参数列表被泛化为接受一个对象,所以它将接受任何东西。它将对该对象执行的操作是将其传递给RouteValueDictionary,然后尝试基于该对象的属性创建查询字符串


如果您说上面的方法有效,您也可以尝试向viewmodel添加一个名为Id的新属性,它将按照您的要求工作。

同时,为了安全起见,我强烈建议您使用一个表单来完成您在此处尝试执行的操作

 @Html.ActionLink("Next", "Lookup", "User", new 
      { Forenames = Model.UserLookupViewModel.Forenames, 
        Surname = Model.UserLookupViewModel.Surname, 
        DOB = Model.UserLookupViewModel.DOB,  
        PostCode = Model.UserLookupViewModel.PostCode, 
        page = Model.UserLookupViewModel.curPage }, null)
MVC将通过适当的方式映射属性;但是,这将使用您的url将值传递给控制器。这将显示所有世界都可以看到的值

为了安全起见,我强烈建议使用表单,尤其是在处理敏感数据(如DOB)时

我个人会这样做:

 @using (Html.BeginForm("Lookup", "User")
 {
     @Html.HiddenFor(x => x.Forenames)
     @Html.HiddenFor(x => x.Surname)
     @Html.HiddenFor(x => x.DOB)
     @Html.HiddenFor(x => x.PostCode)
     @Html.HiddenFor(x => x.curPage)

     <input type="submit" value="Next" />
  }

您必须将模型序列化为JSON字符串,并将其发送到控制器以转换为对象

以下是您的操作链接:

@Html.ActionLink("Next", "Lookup", "User", new { JSONModel = Json.Encode(Model.UserLookupViewModel), page = Model.UserLookupViewModel.curPage }, null)
在控制器中,需要一种方法将JSON数据转换为MemoryStream:

private Stream GenerateStreamFromString(string s)
{
  MemoryStream stream = new MemoryStream();
  StreamWriter writer = new StreamWriter(stream);
  writer.Write(s);
  writer.Flush();
  stream.Position = 0;
  return stream;
}
在ActionResult中,将JSON字符串转换为对象:

public ActionResult YourAction(string JSONModel, int page)
{
  DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Model.UserLookupViewModel));
  var yourobject =  (UserLookupViewModel)ser.ReadObject(GenerateStreamFromString(JSONModel));
}

请尝试此解决方案:

@{
var routeValueDictionary = new RouteValueDictionary("");
routeValueDictionary.Add("Forenames",Forenames);
routeValueDictionary.Add("Surname",Surname);
routeValueDictionary.Add("DoB ",DoB );
routeValueDictionary.Add("Postcode",Postcode );
routeValueDictionary.Add("Page",PageNum );
// Add any item you want!
}
@html.ActionLink("LinkName", "ActionName", "ControllerName",
routeValueDictionary , new{@class="form-control"})

您不能使用ActionLinkMVC4传递模型。MVC4允许您通过URL变量自动传递模型,如我的第二个示例所示。ActionLink的文档显示,该方法的重载都不接受viewmodel对象。根据第二个示例,您可以传递RouteValue,但不能传递整个对象。假设视图模型的属性由页面上的控件填充,我只需发布一个表单,向我们展示您的razor代码。为什么要使用ActionLink而不是发布表单?当所使用的语言无法显示时,请不要使用代码片段进行格式化。这并不能改善答案,而且会让你的答案更加混乱。常规代码块可以正常工作。抱歉,购买HiddenFor不安全。它仍然将数据发送到页面供用户查看,数据在源代码中,只是在页面上不可见。如果没有“安全性”说明,此答案将是对现有答案的一个很好的补充,因为它是唯一使用
Html.BeginForm
Html.HiddenFor
的答案。
public ActionResult YourAction(string JSONModel, int page)
{
  DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Model.UserLookupViewModel));
  var yourobject =  (UserLookupViewModel)ser.ReadObject(GenerateStreamFromString(JSONModel));
}
@{
var routeValueDictionary = new RouteValueDictionary("");
routeValueDictionary.Add("Forenames",Forenames);
routeValueDictionary.Add("Surname",Surname);
routeValueDictionary.Add("DoB ",DoB );
routeValueDictionary.Add("Postcode",Postcode );
routeValueDictionary.Add("Page",PageNum );
// Add any item you want!
}
@html.ActionLink("LinkName", "ActionName", "ControllerName",
routeValueDictionary , new{@class="form-control"})