C# 当控制器不在模型中时,如何将附加值传递给控制器?
我有我的模型C# 当控制器不在模型中时,如何将附加值传递给控制器?,c#,asp.net-mvc,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;
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视图中,我希望用户能够选择一些要附加到创建帖子的现有照片。我使用一些脚本实现了这一点,所以当用户单击submit按钮时,我已经有了一个JSON对象,其中包含将要附加的所有照片ID
但该视图使用Post作为模型。那么我如何从控制器中引用这个对象呢
我想把它转换成字符串并添加一些隐藏的输入。但是否可以从控制器访问此功能
有没有办法在不创建新视图模型的情况下做到这一点
var PhootoIdList = GetAllYourPhotoIds;
var PostModel = {
Title : GetTitle,
Description : GetDescription
};
$.ajax({
url: '/mycontroller/action',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
photoIds: PhotoIdList,
model: {
Post: PostModel
}
}),
success: function(result) {
}
});
没有Ajax
@using(Html.BeginForm("Create", "Posts", FormMethod.Post, null) )
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<select name="photoIds" id="photoIds" multiple>
<option value="AAAA">Photo 1</option>
<option value="BBBB">Photo 2</option>
<option value="CCCC">Photo 3</option>
</select>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Title,Description")] Post post, string[] photoIds)
{
if (ModelState.IsValid)
{
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(post);
}
如果不想创建新的viewmodel,可以向HttpPost操作方法添加新参数以接受文件ID的集合
[HttpPost]
public ActionResult CreatePost(Post model,IEnumerable<int> fileIds)
{
// you can loop through fileIds colleciton
return Json(new { status="success"});
}
理想的解决方案是使用特定于视图的视图模型,而不是将实体模型用作视图模型
public class CreatePostVm
{
public string Title {set;get;}
public string Description {set;get;}
public IEnumerable<int> FileIds {set;get;}
}
上面的jquery代码也可用于将数据发送到此版本的操作方法。是的,这是可能的。
你的行动结果应该是这样的
public ActionResult AddProductToCart_Details(Post post, FormCollection form)
您可以在html中以隐藏名称保存值
<input id="formCheck" type="checkbox" name="xxxx" />
渲染视图时使用的模型与用作控制器操作输入的模型无关。它们可以是不同的模型。您可以将ListPhotoId作为额外参数添加到CreatePost视图中。Public类CreatePostPost,list photoID{…code here}@HastaPasta但在提交表单时如何从视图传递该参数?是否可以不使用ajax?@usingHtml.begformaction,controller,new{photoIds=GetPhotoIdsHere},FormMethod.post,如果不使用ajax和视图模型,是否可以?如果可以,请保持表单字段名与property name.FileIds相同。但我建议您选择视图模型路线。
public ActionResult AddProductToCart_Details(Post post, FormCollection form)
<input id="formCheck" type="checkbox" name="xxxx" />
var day = form["XXXX"];