Passing model values to views MVC 5

会有一股神秘感。 提交于 2020-04-19 14:17:24

问题


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...


回答1:


Lets say you have some models like this

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; }
}

and you want to persist this model from view to view and action to action. In you controller you can create 2 actions. one for your first view and one for your second

Controller

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");
}

in the first action ContactAddress you create a new blank model and pass that in to your view ContactAddress. In that view you can set TempData["currentModel"] equal to the model you are passing in. This will stay in TempData for 1 post back to the server. You dont need to do this on the first page since it's blank anyway but i'm doing it to save time.

View 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>
}

you'll notice in the controller code that the Post Action for ContactAddress is setting a var currentModel equal to what is in TempData["currentModel"] which was set in the ContactAddress view. Before you do a redirect to the next action ContactPhone set TempData["currentModel"] back to the model you are building and use it in the next action.

You do have the option of adding the Model as a parameter to each action and passing the currentModel object like

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

its up to you really. this is not a foolproof way. page refreshes and back and forward buttons could clear out everything that was entered. Storing the information in Session or actually saving the data in a database might be more optimal.




回答2:


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");


来源:https://stackoverflow.com/questions/31682856/passing-model-values-to-views-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!