[RESOLVED]Error: cannot convert from 'string' to 'Oblig3MVC.Models.Spm' and The best overload match for "" has some invalid arguments

Hello.

I have come across a little problem in my code. Im trying to send an argument to a function but it just give me the erros listed in the subject, Anyone out there that maybe see what im doing wrong ? Here is my code

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Oblig3MVC.Models;
using System.Net.Mail;
using System.Threading;
using System.Text;
using System.Net;
using Oblig3MVC;

namespace Oblig3MVC.Controllers
{
    public class ContactformController : Controller
    {

        [HttpGet]
        public ActionResult Contact()
        {
            return View();
        }
        

        // GET: Contactform
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Contact(Bruker bruker)
        {
           
                if (ModelState.IsValid)
                {
                                         
                    try
                    {
                        //Mail funksjon
                        MailMessage msg = new MailMessage();
   
                        msg.To.Add("webapplikasjoners189113@gmail.com");
   
                        MailAddress address = new MailAddress(bruker.Email);
                        msg.From = address;
  
                        msg.Subject = "Spørsmål , Drink.no";
                        msg.Body = bruker.Fornavn + " " + bruker.Etternavn + " " + bruker.Telefon + " " + bruker.Email + " " + bruker.Spørsmål; 
    

 
 
                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                        client.EnableSsl = true; 
   
                        NetworkCredential credentials = new NetworkCredential("webapplikasjoners189113@gmail.com", "webapplikasjoner");
                        client.Credentials = credentials;
 
    
                        client.Send(msg);

                        // Lagre Spørsmål i databasen.

                        var kundeDb = new KundeDB();
                       bool ok = kundeDb.lagreSpm(bruker.Spørsmål); // THIS ONE GIVES THE ERROR
                     
        


                        return View("Success");
                                }                        
                                catch (Exception )
                                {                 
                                 
                                    
                                    return View("Error"); 
                                }
                            }
            return View();    
        }
    }
}

Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Web;
using System.Data.Entity.Core.EntityClient;
using System.Data.Common;

namespace Oblig3MVC.Models
{
    public class Faq
    {
        [Key]
        public int id { get; set; }
        public string problem { get; set; }
        public string losning { get; set; }
        public string katid { get; set; }
        

        public virtual Kategori kategori { get; set; }
    }

    public class Kategori
    {
        [Key]
        public string katid { get; set; }
        public string kategori { get; set; }

        public virtual List<Faq> kunder { get; set; }
    }

    public class  Spm
    {
        [Key]
        public string id { get; set; }
        public string innsendtespørsmål { get; set; }

       
    }

    public class KundeContext : DbContext
    {
        public KundeContext()
          : base("name=Faq")
        {
            Database.CreateIfNotExists();
        }

        // konstruktøren under brukes kun under test!
        public KundeContext(DbConnection connection)
                : base(connection,true)
        {
        }
      
        public DbSet<Faq> Faqs { get; set; }
        public DbSet<Kategori> Kategorier { get; set; }
        public DbSet<Spm> Spms { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }


}

Functions;

  public bool lagreSpm(Spm innSpm)
        {
                var nySpm = new Spm
                {
                    innsendtespørsmål = innSpm.innsendtespørsmål

                };
                try
                {
                    // lagre kunden
                    db.Spms.Add(nySpm);
                    db.SaveChanges();
                }
                catch (Exception feil)
                {
                    return false;
                }
                return true;


            }

stianzz

stianzz

It says that you’re passing in invalid parameter so that please check the input parameter. If they both are match!

Hey thaicarrot and thank you for the reply! :)

Could you maybe elaborate a bit more? From what i can see everything matches just fine.

stianzz

stianzz

public ActionResult Contact(Bruker bruker)         {

bool ok = kundeDb.lagreSpm(bruker.Spørsmål); // THIS ONE GIVES THE ERROR

Or show us your Bruker, I am not sure if the Bruker is domain model

sure. here you go

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

namespace Oblig3MVC.Models
{
    public class Bruker
    {


        [Required(ErrorMessage = "Fornavn må oppgis")]
        [StringLength(50, ErrorMessage = "Maks 50 tegn i fornavn")]
        public string Fornavn { get; set; }
       
        [Required(ErrorMessage = "Etternavn må oppgis")]
        [StringLength(50, ErrorMessage = "Maks 50 tegn i etternavn")]
        public string Etternavn { get; set; }
        
        [Required(ErrorMessage = "Telefonnummer må oppgis")]
        [StringLength(8, ErrorMessage = "Maks 8 tegn i telefonnummeret")]
        public string Telefon { get; set; }

        [Required(ErrorMessage = "E-mailadresse må oppgis")]
        [StringLength(50, ErrorMessage = "Maks 50 tegn for E-mail")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Spørsmål må oppgis")]
        [StringLength(200, ErrorMessage = "Maks 200 tegn per spørsmål.")]
        public string Spørsmål { get; set; }

    }
}

Basically what im trying to do is to send a email with of a contact form and at the same time i want to store the question from the contactform in my database.

Its a schoolproject and the assignment want us to include this function.

Thanks again for any help :) (Assignment is due in 4 hours..)

stianzz

stianzz

Apparently that you’re passing in by using String Type parameter but the Method’s parameter is Type of Spm.

public bool lagreSpm(Spm innSpm)
       
{

It should be as following:

public bool lagreSpm(string innSpm)
       
{

Isn’t?

Ok i tried changing it like that. Its still give error but a different kind of error now.

Error 2 ‘string’ does not contain a definition for ‘innsendtespørsmål’ and no extension method ‘innsendtespørsmål’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?) C:UsersstianDocumentsVisual
Studio 2013ProjectsOblig3MVCOblig3MVCKundeDB.cs 62 48 Oblig3MVC

Just update your code

var nySpm =
new Spm
               
{
                    innsendtesp
ørsmål
= innSpm

                };

Thank you! that did it! :)

Have a great day. You really saved mine :)

Leave a Reply