数据从View传递到Action方法时出现运行时错误

时间:2015-02-25 12:29:39

标签: c# asp.net-mvc-5 entity-framework-6

尝试将数据从视图传递到post方法时,我在浏览器中收到运行时错误。 错误:

  

输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符。

这是我的行动方法:

MyContext db = new MyContext();
public ActionResult SaveGen(Enroll_Gen Enrollment,HttpPostedFileBase file)
{   
    int i = UploadDataInDataBase(file,Enrollment);
    if (i == 1)
    {
        return RedirectToAction("Index");
    }

    return RedirectToAction("Guardian");
}

public int UploadDataInDataBase(HttpPostedFileBase file, Enroll_Gen EnrollGEn)
{
    EnrollGEn.Photo = ConvertToBytes(file);
    var Enroll_Gen = new Enroll_Gen
    {
        AdmnNum = EnrollGEn.AdmnNum,
        Class = EnrollGEn.Class,
        Section = EnrollGEn.Section,
        AcademicYear_Batch = EnrollGEn.AcademicYear_Batch,
        DateOfAdmission=EnrollGEn.DateOfAdmission,
        RollNum =EnrollGEn.RollNum,
        Regt_Numb = EnrollGEn.Regt_Numb,
        Photo = EnrollGEn.Photo,
        FirstName=EnrollGEn.FirstName,
        MiddleName=EnrollGEn.MiddleName,
        LastName=EnrollGEn.LastName,
        DOB=EnrollGEn.DOB,
        Mobile=EnrollGEn.Mobile,
        Gender=EnrollGEn.Gender,
        Nationality=EnrollGEn.Nationality,
        Language=EnrollGEn.Language,
        Religion=EnrollGEn.Religion,
        BloodGroup=EnrollGEn.BloodGroup,
        Address=EnrollGEn.Address,
        Country=EnrollGEn.Country,
        State=EnrollGEn.State,
        City=EnrollGEn.City,
        PinCode=EnrollGEn.PinCode,
        WebUrl=EnrollGEn.WebUrl
    };
    db.Enroll_Gens.Add(Enroll_Gen);
    int i = db.SaveChanges();
    if (i == 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

public byte[] ConvertToBytes(HttpPostedFileBase image)
{
    byte[] imageBytes = null;
    BinaryReader reader = new BinaryReader(image.InputStream);
    imageBytes = reader.ReadBytes((int)image.ContentLength);
    return imageBytes;
}

这是Model类:

public partial class Enroll_Gen
{
    public Enroll_Gen()
    {
        this.Entrance = new HashSet<Entrance>();
        this.QualNAchi = new HashSet<QualNAchi>();
        this.Scholarship = new HashSet<Scholarship>();
    }

    [Key]
    public string AdmnNum { get; set; }
    public string Class { get; set; }
    public string Section { get; set; }
    public int AcademicYear_Batch { get; set; }
    public string DateOfAdmission { get; set; }
    public int RollNum { get; set; }
    public string Regt_Numb { get; set; }
    public byte[] Photo { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string DOB { get; set; }
    public string Mobile { get; set; }
    public string Gender { get; set; }
    public string Nationality { get; set; }
    public string Language { get; set; }
    public string Religion { get; set; }
    public string BloodGroup { get; set; }
    public string Address { get; set; }
    public string Country { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string PinCode { get; set; }
    public string WebUrl { get; set; }

    public virtual ICollection<Entrance> Entrance { get; set; }
    public virtual ICollection<QualNAchi> QualNAchi { get; set; }
    public virtual ICollection<Scholarship> Scholarship { get; set; }
}

0 个答案:

没有答案