【问题标题】:Referencing a checkbox value from HTML to C#将复选框值从 HTML 引用到 C#
【发布时间】:2015-10-14 02:15:07
【问题描述】:

我的 .cshtml 页面上有一个复选框,当它被选中时,我希望它不发送电子邮件给用户。下面是我必须将布尔值从 .cshtml 页面传输到 .cs 邮件程序代码的各个代码页面,但在某处值未传输。请帮忙。

在 View.cshtml 中,我将复选框放入:

<input class="checkbox_priority" id="no_emails" name="no_emails" type="checkbox" value="true" title="This only applies to the 'Complete' button"/>Do not send e-mail to requestor.

在 Notification.cs 我获取/设置值:

public virtual Boolean no_emails { get; set; } //Used to not send an e-mail to the creator of a request.

最后我将 IF 语句放入 RequestService.cs 上的邮件程序代码中:

[Spring.Transaction.Interceptor.Transaction]
    public int Create(int requestId, Policy.Models.Review newReview)
    {
        Policy.Models.Request request;
        Policy.Models.Review review;

        request = RequestDao.Get(requestId);
        newReview.Id = ReviewDao.Save(newReview);

        //Refresh review object
        review = ReviewDao.Get(newReview.Id);
        request.Review = review;

        //Associate review to request
        RequestDao.Update(request);

        //Notify request's author of a response
        string message = "The response to your <a href=\"{0}\">request</a> has been started";
        if (!review.Status.IsActive && !no_emails)
            message = "The response to your <a href=\"{0}\">request</a> has been completed";
        Helpers.Notifier.CreateNotification(request.Author, request, string.Format(message, Helpers.Link.RequestLink(request.Id)));

        return newReview.Id;
    }

CreateNotification 在名为 Notifier.cs 的单独文件中定义

public static void CreateNotification(IList<Policy.Models.User> recipients, Policy.Models.AAuditable subject, string message)
    {
        if (recipients != null && recipients.Count > 0)
        {
            for (int i = 0; i < recipients.Count; ++i)
            {
                CreateNotification(recipients[i], subject, message);
            }
        }
    }

    public static Policy.Models.Notification CreateNotification(Policy.Models.User recipient, Policy.Models.AAuditable subject, string message)
    {

        Policy.Models.Notification note = new Policy.Models.Notification();
        note.Description = message;
        note.IsRead = false;
        note.User = recipient;

但是,它表示 no_emails 在当前上下文中不存在。如何让代码识别 no_emails 具有来自 .cshtml 页面的布尔值?

【问题讨论】:

  • 尝试将复选框名称更改为“no_emails”
  • 我假设您的意思是在if (!review.Status.IsActive &amp;&amp; !no_emails) 行中? - 您在哪里将no_emails 的值传递给该方法?它是Notification 类的属性,并且在方法中没有任何地方引用与该类相关的任何内容 - 您拥有的唯一模型是Review

标签: c# html asp.net-mvc razor checkbox


【解决方案1】:

如果这是 ASP.NET MVC,您可以将 no_emails 变量放在 Action 参数列表中,MVC 会为您绑定到 POSTed 表单值。但是,表单上的参数名称和name 属性必须匹配。在这里,您有 name="emails",因此您需要更改它以匹配您使用的参数名称。

但是你有一个 Spring 属性让我觉得这不是 ASP.NET MVC?

【讨论】:

  • 这不是我的代码,我只是在修改它。我相当肯定它是 ASP.NET MVC。
【解决方案2】:

这对我来说听起来像是一个范围问题。我需要查看更多代码才能确定,但​​让我们假设在 RequestService 中创建了一个 Notification 实例。您不能直接访问变量 no_emails。你可以这样访问:

// here is the instance of notification
Notification someNotifacation = new Notification();

// call the getter method to get the value
if(!someNotification.no_emails)
{
 ...
}

【讨论】:

  • 您希望看到更多代码的哪一部分?我编辑了我的帖子以包含更多内容,请告诉我是否有您能想到的具体内容。
  • 我不确定您在提交表单时对它做了什么。你能把变量 no_emails 放在请求对象中吗?然后你可以在 Create 方法中检查它的值。
猜你喜欢
  • 2015-12-05
  • 2023-03-04
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
相关资源
最近更新 更多