Passing model values to views MVC 5

时间:2015-07-28 17:06:03

标签: asp.net-mvc model-view-controller model views

I am in need of how the correct way to do this.

  1. I can not use forms authentication
  2. A user will "login" or confirm identity based on a value

I need to walk the user through a series of pages like so

  1. Contact/MailAddress
  2. Contact/Phone
  3. Contact/Email
  4. Contact/Summary
  5. Questionaire/Question1
  6. Questionaire/Question2
  7. Questionaire/Question3
  8. Questionaire/Summary
  9. Final/Certify
  10. Final/Review

I plan on using Session to hold the data but I'm having trouble figuring out how to pass the values to other views and how Redirect to other pages.

Any help will do...

2 个答案:

答案 0 :(得分:1)

假设您有一些像这样的模型

public class ContactModel 
{
    public string MailAddress { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

public class QuestionaireModel
{
    public string Question1Answer { get; set; }
    public string Question2Answer { get; set; }
    public string Question3Answer { get; set; }
}

public class ContactQuestionaireModel
{
    public ContactModel Contact { get; set; }
    public QuestionaireModel Question { get; set; }
}

并且您希望将此模型从视图持久化到视图和操作到操作。在您的控制器中,您可以创建2个操作。一个用于第一个视图,一个用于第二个视图

<强>控制器

public ActionResult ContactAddress()
{
    var model = new ContactQuestionaireModel();
    return View(model);
}

[HttpPost]
public ActionResult ContactAddress(ContactQuestionaireModel model)
{
    var currentModel = TempData["currentModel"] as ContactQuestionaireModel;
    currentModel.Contact.MailAddress = model.Contact.MailAddress;
    TempData["currentModel"] = currentModel;
    return RedirectToAction("ContactPhone");
}

public ActionResult ContactPhone()
{
    var model = TempData["currentModel"] as ContactQuestionaireModel;
    return View(model);
}

[HttpPost]
public ActionResult ContactPhone(ContactQuestionaireModel model)
{
    var currentModel = TempData["currentModel"] as ContactQuestionaireModel;
    currentModel.Contact.Phone = model.Contact.Phone;
    TempData["currentModel"] = currentModel;
    return RedirectToAction("ContactEmail");
}

在第一个操作ContactAddress中,您创建了一个新的空白模型并将其传递到您的视图ContactAddress。在该视图中,您可以将TempData [&#34; currentModel&#34;]设置为等于您传入的模型。这将保留在TempData中1个帖子返回服务器。你不需要在第一页上这样做,因为它无论如何都是空白的,但我这样做是为了节省时间。

查看ContactAddress

@model WebApplication3.Models.ContactQuestionaireModel
@{
    ViewBag.Title = "Contact Address";
    TempData["currentModel"] = Model; //this will be available to me in the HttpPost action
}

@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.LabelFor(m => m.Contact.MailAddress, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Contact.MailAddress, new { @class = "form-control" })
        </div>
    </div>
    <button type="submit">Submit</button>
}

您将在控制器代码中注意到ContactAddress的发布操作正在设置var currentModel等于TempData中的内容[&#34; currentModel&#34;]在ContactAddress视图中设置。在您重定向到下一个操作ContactPhone之前,请将TempData["currentModel"]设置回您正在构建的模型并在下一个操作中使用它。

您可以选择将模型作为参数添加到每个操作并传递currentModel对象,如

public ActionResult ContactPhone(ContactQuestionaireModel model)
return RedirectToAction("ContactPhone", currentModel);

真的取决于你。这不是一个万无一失的方式。页面刷新和后退和前进按钮可以清除输入的所有内容。将信息存储在Session中或实际将数据保存在数据库中可能更为理想。

答案 1 :(得分:0)

I advise against doing what you are attempting to do by logging in with session but what you are looking for is:

TempData.yourModel = new SomeModel { Data = "yourData" };

//next page
var model = (SomeModel)TempData.yourModel;

and

RedirectToAction("yourController", "yourAction");