我有我的模型
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/