使用动态.aspx页面ASP.NET MVC 3将数据保留回ViewModel

时间:2021-07-05 04:04:05

I have a view that is created dynamically with objects from my database. How can I take the values from my View and pass them to my view model if it is created dynamically? I know this question is vague but hopefully looking through my code will help you help me.

我有一个使用我的数据库中的对象动态创建的视图。如果动态创建,我如何从View中获取值并将它们传递给我的视图模型?我知道这个问题很模糊,但希望通过我的代码查看会帮助你。

View

视图

  <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<h1>Survey Says...</h1>
<ol>       
<li>
    Name: <input type="text" disabled="true" name="name" value="<%= Model.Name %>"/> 
</li>                                                  

<li>
    Email: <input type="text" disabled="true" name="email" value="<%= Model.Email %>"/>
</li>

<%foreach(SurveyQuestions s in Model.myQuestions)
  { %>   

    <li>    <%= s.QuestionText %>    <br />
    <%
    foreach(SurveyQuestionAnswers q in s.QuestionAnswers)
    {

    %>
        <input type="radio" name="rbGroup<%= q.Id%>"/> <%= q.DisplayText %><br/>

        <% 
     if(q.IsEditable)
     {
         %>
            <input type="text" id="txtOther<%= q.Id %>"/>
         <%
     }
    }
      %>           


    </li>

 <% } 

  %>      
           <li>
    <input type="submit" value="Save" id="save-button" />
</li>

</ol>
<% } %>

SurveyQuestion class

SurveyQuestion类

public class SurveyQuestions
{
    public string QuestionText { get; set; }
    public List<SurveyQuestionAnswers> QuestionAnswers { get; set; }
    public int Id { get; set; }
}

SurveyQuestionAnswers class

SurveyQuestionAnswers类

 public class SurveyQuestionAnswers
{
    public int Id { get; set; }
    public string DisplayText { get; set; }
    public bool IsEditable { get; set; }
}

My sloppy ViewModel

我邋View的ViewModel

          public class GfcPreInterventionSurveyViewModel
{
    static SurveyService myService = new SurveyService(new SurveyRepository());

    [DisplayName("Name")]
    [Required(ErrorMessage = "Name is required.")]
    public string Name { get; set; }

    [DisplayName("Work Email")]
    [Email(ErrorMessage = "The email you entered is not valid.")]
    public string Email { get; set; }

    [DisplayName("Gender")]
    [Required(ErrorMessage = "Gender is required.")]
    public string Gender { get; set; }


    [DisplayName("Country")]
    [Required(ErrorMessage = "Country is required.")]
    public string Country { get; set; }

    [DisplayName("Business")]
    [Required(ErrorMessage = "Please select a business unit.")]
    public string BusinessUnit { get; set; }

    public SelectList GenderList;
    public SelectList BusinessList;
    public SelectList CountryList;
    public SelectList RoutineList;
    public SelectList ReasonList;
    public SelectList ActivityList;
    public SelectList HealthList;
    public SelectList EnergyList;

    public SelectListItem GenderItem;
    public SelectListItem BusinessItem;
    public SelectListItem CountryItem;
    public SelectListItem RoutineItem;
    public SelectListItem ReasonItem;
    public SelectListItem ActivityItem;
    public SelectListItem HealthItem;
    public SelectListItem EnergyItem;


    public Boolean ToGetFit { get; set; }
    public Boolean ToChallengeMyself { get; set; }
    public Boolean ToIncrEnergy { get; set; } 
    public Boolean ToBuildMorale { get; set; }
    public Boolean ToBeHealthier { get; set; }
    public Boolean ChallengeOther { get; set; }

    public string OtherString { get; set; }    

    [DisplayName("Exercise Routine")]
    [Required(ErrorMessage = "Which option describes your exercise routine.")]
    public string Routine { get; set; }


    [DisplayName("Physical Activity")]
    [Required(ErrorMessage = "Which option describes your physical activity.")]
    public string Activity { get; set; }


    [DisplayName("Overall Health")]
    [Required(ErrorMessage = "Which option describes your overall health.")]
    public string Health { get; set; }

    [DisplayName("Energy")]
    [Required(ErrorMessage = "Which option best describes your energy.")]
    public string Energy{ get; set; }

    public int ReasonsForChallenge { get; set; }



    public List<SurveyQuestions> myQuestions = new List<SurveyQuestions>();
    //public List<SurveyQuestionAnswers> myAnswers;


    public void build(int id)
    {
        var myService = new SurveyService(new SurveyRepository());

        myQuestions = myService.GetSurveyQuestions(id);

            }

my getSurveyQuestions method returns a List of SurveyQuestions objects.

我的getSurveyQuestions方法返回一个SurveyQuestions对象列表。

currently, in my controller, when save is hit, the post method is called. this is where i want to update my database with the values in the viewmodel, but because my page is so dynamic, i am having trouble accessing the user's input.

目前,在我的控制器中,当命中保存时,调用post方法。这是我想用viewmodel中的值更新我的数据库的地方,但由于我的页面是如此动态,我无法访问用户的输入。

My Controller:

我的控制器:

public class SurveyController : WidgetControllerBase
{
    #region Private Members
    private readonly ISurveyRepository _surveyRepository;
    #endregion

    #region Constructors

    public SurveyController(ISurveyRepository surveyRepository)
    {
        if (surveyRepository == null)
        {
            throw new ArgumentNullException("surveyRepository");
        }
        _surveyRepository = surveyRepository;
    }

    #endregion

    // GET: /Survey/GfcPreIntervention
    public ActionResult GfcPreInterventionSurvey()
    {

        var surveyService = new SurveyService();
        var vm = new GfcPreInterventionSurveyViewModel();

        vm.build(AppUserInstance.Id);

        //paymentService.SetDVDShipped(payment.PaymentId);
        return View(vm);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GfcPreInterventionSurvey(GfcPreInterventionSurveyViewModel viewModel)       
    {
        return View(viewModel);                     
    }
}

1 个解决方案

#1


1  

Since your page is highly dynamic, maybe you would be better off writing a custom model binder. Though the term sounds intimidating it’s not actually that complicated.

由于您的页面非常动态,也许您最好不要编写自定义模型绑定器。虽然这个词听起来令人生畏但实际上并不那么复杂。

You would simply create a class which would implement IModelBinder. Within this class, you would have access to the form collection and can populate any complex object(s) with it

您只需创建一个实现IModelBinder的类。在此类中,您可以访问表单集合,并可以使用它填充任何复杂对象

Following are some examples worth looking at

以下是一些值得关注的例子

http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

http://ivanz.com/2010/11/03/custom-model-binding-using-imodelbinder-in-asp-net-mvc-two-gotchas/

http://ivanz.com/2010/11/03/custom-model-binding-using-imodelbinder-in-asp-net-mvc-two-gotchas/

And as you have figured out, using helper methods (preferably with Razor) will clean up the UI code

正如您所知,使用辅助方法(最好使用Razor)将清除UI代码

#1


1  

Since your page is highly dynamic, maybe you would be better off writing a custom model binder. Though the term sounds intimidating it’s not actually that complicated.

由于您的页面非常动态,也许您最好不要编写自定义模型绑定器。虽然这个词听起来令人生畏但实际上并不那么复杂。

You would simply create a class which would implement IModelBinder. Within this class, you would have access to the form collection and can populate any complex object(s) with it

您只需创建一个实现IModelBinder的类。在此类中,您可以访问表单集合,并可以使用它填充任何复杂对象

Following are some examples worth looking at

以下是一些值得关注的例子

http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

http://ivanz.com/2010/11/03/custom-model-binding-using-imodelbinder-in-asp-net-mvc-two-gotchas/

http://ivanz.com/2010/11/03/custom-model-binding-using-imodelbinder-in-asp-net-mvc-two-gotchas/

And as you have figured out, using helper methods (preferably with Razor) will clean up the UI code

正如您所知,使用辅助方法(最好使用Razor)将清除UI代码