c# - 当它不在模型中时如何将附加值传递给 Controller ​​?

标签 c# asp.net-mvc

我有我的模型

public class Post
{
    [Key]
    public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }
}

public class Photo
{
    [Key]
    public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string Source { get; set; }
}

public class PhotoAttach
{
    [Key]
    public int Id { get; set; }

    public virtual Post Post { get; set; }

    public virtual Photo Photo { get; set; }

    public bool IsThumbnail { get; set; }
}

在我的 CreatePost View 中,我希望用户能够选择一些现有照片以附加到创建帖子中。我使用一些脚本制作了这个,所以当用户点击提交按钮时,我已经有了一个包含所有要附加的照片 ID 的 JSON 对象。

但是这个 View 使用Post 作为模型。那么如何从 Controller 中引用这个对象呢?

我考虑将其转换为字符串并添加一些隐藏的输入。但是可以从 Controller 访问它吗?

有没有办法在不创建新 View 模型的情况下做到这一点?

最佳答案

是的,这是可能的。 你的操作结果是这样的

 public ActionResult AddProductToCart_Details(Post post, FormCollection form)

您可以将值保存在html中的隐藏名称中。

<input id="formCheck" type="checkbox" name="xxxx" /> 

然后像这样获取该值。

var day = form["XXXX"];

关于c# - 当它不在模型中时如何将附加值传递给 Controller ​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34474987/

相关文章:

c# - Blazor - 多个参数路由(带逗号)导致异常

c# - 网络流接收数据与 '\0' 混合

asp.net-mvc - ADFS 2.0 Windows 2008 R2 Web API

asp.net - 我收到asp.net应用程序的运行时错误,是否是超时问题?

asp.net-mvc - SignalR 与 setTimeout

javascript - 如何在通过 Ajax.ActionLink 调用的动态加载的 ASP.NET MVC 部分 View 上初始化 jsTree

C# 如何使用 libgit2Sharp 读取提交时间

c# - 为什么我不能用 linq 迭代可以用 foreach 迭代的东西?

c# - 为什么当我单击滚动条时控制台会暂停我的代码

c# - Webmatrix - 将变量从 cshtml 页面传递到 aspx 页面