CS0103:名称在当前上下文中不存在?

时间:2019-03-07 09:46:46

标签: c# validation model-view-controller contacts

我不确定为什么,因为该方法可以在我完成的其他项目中使用(据我所知)

但是由于某种原因,它一直说“ Mail ...”这个名称在当前上下文中不存在,我不理解。

因此,如果有人可以弄清为什么会发生这种情况,以及如何解决它,我将不胜感激!

当我尝试验证联系表单中的输入时,似乎出现了问题。

(本来我想为Contacts建模一个,为SQL中的表建模,但我无法使其正常工作)

控制器:

public ActionResult Kontakt()
{
    var ViewModel = new Fisk.Models.ALL();

    ViewModel.Kontakt = db.web_Kontakt.FirstOrDefault();
    ViewModel.Kategori = db.site_Kategorier.FirstOrDefault();

    ViewModel.kategorier = db.site_Kategorier.ToList();

    List<site_Kategorier> kategorier = new List<site_Kategorier>();

    return View(ViewModel);
}

[HttpPost]
public ActionResult Kontakt(Fisk.Models.ALL myMail)
{
    var ViewModel = new Fisk.Models.ALL();

    ViewModel.Kontakt = db.web_Kontakt.FirstOrDefault();
    ViewModel.Kategori = db.site_Kategorier.FirstOrDefault();

    ViewModel.kategorier = db.site_Kategorier.ToList();

    List<site_Kategorier> kategorier = new List<site_Kategorier>();

    if (ModelState.IsValid)
    {
        SendMail(myMail);
    }

    return View(ViewModel);
}

public void SendMail(Fisk.Models.ALL myMail)
{
    MailMessage mail = new MailMessage();

    // Afsender
    mail.From = new MailAddress(myMail.MailEmail);

    // Besvarelsen sendes til... 
    mail.ReplyToList.Add(myMail.MailEmail);

    // Emne / overskrift 
    mail.Subject = "Fisk:" + myMail.MailOverskrift;

    // Modtager 
    mail.To.Add("webtestmastermail@gmail.com");

    // Mail body
    mail.Body = myMail.MailNavn + myMail.MailBesked.Replace(Environment.NewLine, "<br />");

    // HTML = tillader HTML
    mail.IsBodyHtml = true;

    //Teknisk afsendelse
    SmtpClient smtp = new SmtpClient();

    //Udbyders mailhost
    smtp.Host = "smtp.gmail.com";

    // Port
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;

    // Email + Password
    smtp.Credentials = new System.Net.NetworkCredential("webtestmastermail@gmail.com", "***");

    // Send
    smtp.Send(mail);
}

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Fisk.Models
{
    public class ALL
    {

        public List<front_Slider> Slider { get; set; }
        public List<front_TekstBoks> tekstBoks { get; set; }
        public List<site_Kategorier> kategorier { get; set; }

        public List<site_Product> products { get; set; }

        public web_Kontakt Kontakt { get; set; }
        public site_Kategorier Kategori { get; set; }

        public site_Product o_product { get; set; }


        // NAVN
        [Required(ErrorMessage = "Fejl: Der mangler at indsætte et navn")]
        public string MailNavn { get; set; }

        // EMAIL
        [Required(ErrorMessage = "Fejl: Der skal indsætte en e-mail")]
        public string MailEmail { get; set; }

        // EMNE
        [Required(ErrorMessage = "Fejl: Der mangler en overskrift for beskeden")]
        public string MailOverskrift { get; set; }

        // BESKED
        [Required(ErrorMessage = "Fejl: Der mangler en tekst for beskeden ")]
        public string MailBesked { get; set; }
    }
}

查看:

@model Fisk.Models.ALL

@{
    ViewBag.Title = "Kontakt";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="k_container conAll">
    <h2>Kontakt Fisk.nu</h2>

    <form action="/Home/Kontakt" method="post">
        <label for="MailNavn">Navn:</label>
        <input type="text" id="MailNavn" name="MailNavn">
        <p>@Html.ValidationMessage(MailNavn)</p>>

        <label for="MailEmail">E-mail:</label>
        <input type="text" id="MailEmail" name="MailEmail">
        <p>@Html.ValidationMessage(MailEmail)</p>

        <label for="MailOverskrift">Emne:</label>
        <input type="text" id="MailOverskrift" name="MailOverskrift">
        <p>@Html.ValidationMessage(MailOverskrift)</p>

        <textarea name="MailBesked" id="MailBesked"></textarea>
        <p>@Html.ValidationMessage(MailBesked)</p>
        <input type="submit" name="Submit" value="Send" />
    </form>

    <div id="mapCon">
        <div id="mapFrame">
            <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2222.554893407964!2d10.211985315916563!3d56.14750998066078!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zNTbCsDA4JzUxLjAiTiAxMMKwMTInNTEuMCJF!5e0!3m2!1sda!2sdk!4v1551903974778" width="400" height="320" frameborder="0" style="border:0" allowfullscreen></iframe>
        </div>
        <p>GPS: 56.14751, 10.214174</p>
    </div>
</div>

0 个答案:

没有答案