我在View中有一个对象,我想将它传递给控制器的操作。
此对象不在数据库中,因此(在另一个操作中创建并传递给此View)我希望使用@Html方法传递整个对象(创建一个按钮并在单击该操作时传递它)。
例如,视图中有Type object变量。如何通过单击一个按钮来传递它(对象)。我想使用@Html.*方法,而不是Java脚本。
控制器名称:MyController
public ActionResult MyAction(Type ReceivedObject) {
return View();
}编辑
所以我不知道该怎么办了。
在这里,我创建了一个元素列表,这些元素是数据库中元素的副本(数据库中有两个调查)。我把它们传递给_Survey1.cshtml视图
[HttpPost]
public ActionResult _Survey1(int id) {
System.Diagnostics.Debug.WriteLine("PASSED ID: " + id);
Person person = db.Persons.Find(id);
//Passing a Tuple to Partial View
List<Survey> localSurveysCopy = new List<Survey>();
foreach (Survey survey in db.Surveys) {
localSurveysCopy.Add(new Survey(survey));
}
var tuple = new Tuple<Person, List<Survey>>(person, localSurveysCopy) { };
return PartialView(tuple);
}这些元素在_Survey1.cshtml视图中被修改。用户单击如下所示的无线电按钮:

调查中的每一个问题都有编辑助手,它定义了用AnswerText属性保存Question的答案。
@model WebApplication2.Models.Question
<div>
@Html.HiddenFor(x => x.Id)
<h3> @Model.QuestionText </h3>
@foreach (var a in Model.Answers) {
<p>
@Html.RadioButtonFor(b => b.SelectedAnswer, a.Id) @a.AnswerText
</p>
}
</div>这就是呈现调查的_Survey1视图:
@using WebApplication2.Models
@model System.Tuple<Person, List<Survey>>
<hr />
<h1>Surveys</h1>
<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
@{int i = 1;}
@foreach (var survey in Model.Item2) {
using (Html.BeginForm()) {
<h2>Survey @(i)</h2>
<p />
@Html.EditorFor(x => survey.Questions)
// <p>@Html.ActionLink("Call Cell Phone", "SubmitSurvey", new {survey = survey}, new { @class = "btn btn-default" })</p>
}
i++;
<hr style="background-color:rgb(126, 126, 126);height: 5px" />
}
<hr />下面是我想将模型的一部分传递给的方法,这两个动作都在同一个控制器中。
public ActionResult SubmitSurvey(Person id, List<Survey> listOfSurveys) {
//BECAUSE OF ID's of copied Surveys and questions and answers in the
// listOfSurveys are the same as Id's of original Questions
// in database I want to bind Selected Answers with the Persons here
//that is why I need this list
return View();
}传递Person id很容易,但是如何传递这个列表呢?
下面是Survey、Question和Answer的样子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.Models {
public class Survey {
public int Id { set; get; }
public virtual ICollection<Question> Questions { set; get; }
public Survey() { }
public Survey(Survey survey) {
Id = survey.Id;
Questions = new List<Question>();
System.Diagnostics.Debug.WriteLine("SURVEY " + survey.Questions == null);
foreach (Question question in survey.Questions) {
Questions.Add(new Question(question));
}
}
}
public class Question {
public int Id { set; get; }
public string QuestionText { set; get; }
public virtual ICollection<Answer> Answers { set; get; }
public virtual Survey Survey { get; set; }
public string SelectedAnswer { set; get; } //this field is SET after clicking SAVE button
public Question() { }
public Question(Question question) {
Id = question.Id;
QuestionText = question.QuestionText;
Answers = question.Answers;
Survey = question.Survey;
SelectedAnswer = "";
}
}
public class Answer {C:\Users\R\documents\visual studio 2013\Projects\WebApplication2\WebApplication2\Models\Survey.cs
public int Id { set; get; }
public string AnswerText { set; get; }
public virtual Question Question { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
}和Person谁记得哪一个答案是这样选择的(很多到许多关系Answer<---> Person
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebApplication2.Models {
public class Person {
public int Id { get; set; }
[DisplayName("First Name")]
public string FirstName { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
}发布于 2014-09-01 17:39:19
你不能,或者至少你不能用你的思维方式。
首先,这与助理员无关。帮助程序只生成HTML,因此它们受到HTML约束的约束。其次,您需要生成的URL本身受HTTP约束的约束。如果您正在讨论执行GET请求(您在HTML中标准运行磨锚链接),那么传递数据的唯一方法就是通过查询字符串。这就是你有时在URL中看到的?foo=bar&bar=baz爵士乐。查询字符串只支持简单的名称-值对。无法将完整对象作为单个参数传递,尽管可以单独传递该对象的每个属性。MVC中的模型绑定器足够聪明地获取查询字符串的各个项,并将它们重新连接到它们所属的类中(如果它们能够匹配它们)。例如,假设您有一个类,如:
public class Foo
{
public string Bar { get; set; }
public int Baz { get; set; }
}还有这样的行动:
public ActionResult Foo(Foo model)
{
...
}您可以生成一个可以通过以下方式工作的链接:
@Html.ActionLink("This is a foo.", "Foo", new { Bar = "Bar", Baz = 1 })模型绑定器将认识到它在查询字符串中有Bar和Baz的值,并使用这些值创建Foo实例。
如果您有一个POST请求,只能使用method设置为"post“的HTML,那么您可以以类似的方式包含类的属性,直到现在,它们才是实际的HTML输入,而不是直接传递到查询字符串中。例如:
@using (Html.BeginForm("Foo")) {
<input type="hidden" name="Bar" value="Bar" />
<input type="hidden" name="Baz" value="1" />
<button type="submit">Submit</button>
}同样,模型绑定器将匹配属性名,并使用这些值构造类的实例。但是,您不能简单地传递一个对象,而只能单独传递该对象的属性。即使有了POST,HTML本身也只能提交一个x-www-form-urlencoded帖子体,这只是你在GET请求中看到的查询字符串样式的花哨名称。
要发布一个实际的对象,您必须使用一些数据交换格式,如JSON或XML。而且,这只能在JavaScript的帮助下完成,这是您明确不想要的。
https://stackoverflow.com/questions/25610512
复制相似问题