在 MVC2 中获取 checkKbox 值

发布于 2024-10-30 14:07:26 字数 2475 浏览 1 评论 0原文

我正在尝试显示动态复选框列表并允许用户选择一个或多个。返回控制器后,我需要获取所有已检查的 ID 并将每个记录写入数据库。

以下是与此相关的代码片段。

DTO

public class OfficeVisitPartOfBodyDisplay
{
    public int PartOfBodyId { get; set; }
    public string PartOfBodyName { get; set; }
    public bool PartOfBodyChecked { get; set; }
}

模型

public class OfficeVisitModel
{
    public OfficeVisitEntity OfficeVisit { get; set; }
    public TList<PartOfBodyEntity> PartOfBodies { get; set; }
    public TList<OfficeVisitPartOfBodyEntity> OfficeVisitPartOfBodies { get; set; }
    public List<OfficeVisitPartOfBodyDisplay> OfficeVisitPartOfBodyDisplays { get; set; }
    public string PatientName { get; set; }
}

视图(有问题的部分)

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
    <legend>Pain Area(s):</legend>
        <% foreach (OfficeVisitPartOfBodyDisplay officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays)
                { %>
                    <label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>"><%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
                    <input type="checkbox" id="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId%>" name="partofbodydisplay" value="<%= officeVisitPartOfBodyDisplay.PartOfBodyName%>" />
        <% } %>                 
    </fieldset>
</div>

控制器、模型中的 OfficeVisitPartOfBodyDisplays 和 partofbodydisplay 总是返回没有数据的数据。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ActivePainArea(OfficeVisitModel model, OfficeVisitPartOfBodyDisplay[] partofbodydisplay, string submitButton)
{
    switch (submitButton)
    {
        case "Save":
            model.Message = "Save Coming Soon";
            return View(model);
        case "Cancel":
            model.Message = "Cancel Coming Soon";
            return View(model);
        case "Complaint":
            return RedirectToAction("ActiveComplaint", new { patientId = model.OfficeVisit.PatientId, readOnly = false });
        case "Patient History":
            model.Message = "Patient History Coming Soon";
            return View(model);
            //return RedirectToAction("ActivePatientHistory", new { patientId = model.OfficeVisit.PatientId });
        default:
            return View(model);
    }
}

I am trying to display a list of dynamic check boxes and allow the user to select one or more. Once returned to the controller, I would need to take the ids of all the checked ones and write a record for each to the database.

Below are the pieces of code that relate to this.

DTO

public class OfficeVisitPartOfBodyDisplay
{
    public int PartOfBodyId { get; set; }
    public string PartOfBodyName { get; set; }
    public bool PartOfBodyChecked { get; set; }
}

Model

public class OfficeVisitModel
{
    public OfficeVisitEntity OfficeVisit { get; set; }
    public TList<PartOfBodyEntity> PartOfBodies { get; set; }
    public TList<OfficeVisitPartOfBodyEntity> OfficeVisitPartOfBodies { get; set; }
    public List<OfficeVisitPartOfBodyDisplay> OfficeVisitPartOfBodyDisplays { get; set; }
    public string PatientName { get; set; }
}

View (The part in question)

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
    <legend>Pain Area(s):</legend>
        <% foreach (OfficeVisitPartOfBodyDisplay officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays)
                { %>
                    <label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>"><%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
                    <input type="checkbox" id="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId%>" name="partofbodydisplay" value="<%= officeVisitPartOfBodyDisplay.PartOfBodyName%>" />
        <% } %>                 
    </fieldset>
</div>

Controller, the OfficeVisitPartOfBodyDisplays in the model and the partofbodydisplay always come back with no data.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ActivePainArea(OfficeVisitModel model, OfficeVisitPartOfBodyDisplay[] partofbodydisplay, string submitButton)
{
    switch (submitButton)
    {
        case "Save":
            model.Message = "Save Coming Soon";
            return View(model);
        case "Cancel":
            model.Message = "Cancel Coming Soon";
            return View(model);
        case "Complaint":
            return RedirectToAction("ActiveComplaint", new { patientId = model.OfficeVisit.PatientId, readOnly = false });
        case "Patient History":
            model.Message = "Patient History Coming Soon";
            return View(model);
            //return RedirectToAction("ActivePatientHistory", new { patientId = model.OfficeVisit.PatientId });
        default:
            return View(model);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-11-06 14:07:26

尝试一下:

控制器:

public ActionResult ActivePainArea(OfficeVisitModel officeVisitPartOfBodyDisplay, string submitButton) {

视图:

<div data-role="fieldcontain">
    <% using (Html.BeginForm()) { %>
    <fieldset data-role="controlgroup">
      <legend>Pain Area(s):</legend>
      <% var i = 0; %>
      <% foreach (var officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays) { %>
      <label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>">
        <%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
        <%: Html.CheckBox("OfficeVisitPartOfBodyDisplays[" + i.ToString() + "].PartOfBodyChecked", officeVisitPartOfBodyDisplay.PartOfBodyChecked)%>
        <% i++; %>
      <% } %>
      <input type="submit" value="save" />
    </fieldset>
    <% } %>
  </div>

此外,您可能还想参考此页面以获取更多信息:
Phil Haacked 模型绑定到列表

give this a try:

Controller:

public ActionResult ActivePainArea(OfficeVisitModel officeVisitPartOfBodyDisplay, string submitButton) {

View:

<div data-role="fieldcontain">
    <% using (Html.BeginForm()) { %>
    <fieldset data-role="controlgroup">
      <legend>Pain Area(s):</legend>
      <% var i = 0; %>
      <% foreach (var officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays) { %>
      <label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>">
        <%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
        <%: Html.CheckBox("OfficeVisitPartOfBodyDisplays[" + i.ToString() + "].PartOfBodyChecked", officeVisitPartOfBodyDisplay.PartOfBodyChecked)%>
        <% i++; %>
      <% } %>
      <input type="submit" value="save" />
    </fieldset>
    <% } %>
  </div>

Also you might want to reference this page for more information:
Phil Haacked Model Binding To A List

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文