Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I am trying to write a save function in my asp.net mvc application. It works fine, but I had to change
C#
public ActionResult Index(List<Doctor> drnos, int? page)
to
C#
public ActionResult save(List<Doctor> drnos, int? page)
because the function was being called when the index loaded. The problem is that it does not like that I am referencing "save" instead of "Index". I already have an Index function that does multiple things including the implementation of a search bar. I do not want the save function to be called every time the index is loaded. What am I doing wrong in my "save" function? Thanks!

drnosController
C#
public ActionResult save()
       {
           return View();

       }

       [HttpPost]
       public ActionResult save(List<Doctor> drnos, int? page)
       {

           System.Diagnostics.Debug.WriteLine("Save Called");
           DrNOSv2Entities db = new DrNOSv2Entities();
           foreach (Doctor doc in drnos)
           {

               Doctor existing = db.Doctors.Find(doc.RVH_ID_);
               if (existing != null)
               {
                   System.Diagnostics.Debug.WriteLine("passed if");
                   existing.AdmPriv = doc.AdmPriv;
                   existing.QCPR = doc.QCPR;
                   existing.Keane = doc.Keane;
                   existing.Orsos = doc.Orsos;
                   existing.Soft = doc.Soft;
                   existing.C3M = doc.C3M;

               }
           }
           db.SaveChanges();
          int pageSize = 100;
           int pageNumber = (page ?? 1);
           return View(drnos.ToPagedList(pageNumber, pageSize));

       }


Index.cshtml
C#
@using (Html.BeginForm("save", "drnos",  FormMethod.Post, new { id = "doctorform" }))
 {
    

     for (int i = 0; i < Model.Count; i++)
     {

            <tr>
                <td>
                    
                    @Model[i].RVH_ID_
                    @Html.HiddenFor(m => m[i].RVH_ID_)

                </td>
                <td>
                    @Model[i].Last_Name
                    @Html.HiddenFor(m => m[i].Last_Name)
                </td>
                <td>
                    @Model[i].First_Name
                    @Html.HiddenFor(m => m[i].First_Name)
                </td>
                <td>
                    @Model[i].Middle_Name
                    @Html.HiddenFor(m => m[i].Middle_Name)
                </td>
                <td>
                    @Model[i].Degree1
                    @Html.HiddenFor(m => m[i].Degree1)
                </td>
                <td>
                    @Model[i].Group
                    @Html.HiddenFor(m => m[i].Group)
                </td>
                <td>
                    
                    @Html.CheckBoxFor(m => m[i].AdmPriv)
                </td>
                <td>
                    @Html.CheckBoxFor(m => m[i].QCPR)
                </td>
                <td>
                    @Html.CheckBoxFor(m => m[i].Keane)

                </td>
                <td>
                    @Html.CheckBoxFor(m => m[i].Orsos)

                </td>
                <td>
                    @Html.CheckBoxFor(m => m[i].Soft)

                </td>
                <td>
                    @Html.CheckBoxFor(m => m[i].C3M)
                </td>


                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = Model[@i].RVH_ID_ })  
                    @Html.ActionLink("Details", "Details", new { id = Model[@i].RVH_ID_ })

                </td>
            </tr>
     
            }
            
            }

        </table>


Doctor.cs
C#
namespace drnosv6.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Doctor
    {
        public int RVH_ID_ { get; set; }
        public string NPI { get; set; }
        public bool On_Staff { get; set; }
        public bool AdmPriv { get; set; }
        public bool Inactive { get; set; }
        public string License_Verification_Date { get; set; }
        public string Last_Name { get; set; }
        public string First_Name { get; set; }
        public string Middle_Name { get; set; }
        public string Group { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Zip { get; set; }
        public string State { get; set; }
        public string Telephone { get; set; }
        public string Fax { get; set; }
        public string Phys_Spclty { get; set; }
        public string License_ { get; set; }
        public string UPIN { get; set; }
        public bool QCPR { get; set; }
        public bool Keane { get; set; }
        public bool Orsos { get; set; }
        public bool C3M { get; set; }
        public bool Soft { get; set; }
        public bool DIC { get; set; }
        public bool Metalink { get; set; }
        public bool Open_Med { get; set; }
        public bool Muse { get; set; }
        public bool MedQuest { get; set; }
        public string Degree1 { get; set; }
        public string Degree2 { get; set; }
        public string Degree3 { get; set; }
    }
}
Posted
Updated 9-Jun-15 4:28am
v2
Comments
Jameel VM 9-Jun-15 13:06pm    
here mvc expecting the view named 'Index'. I think you renamed Index to Save. If it is renamed please the view name from Index to Save
britbarnes 9-Jun-15 14:08pm    
How do I have two different functions in controller named public ActionResult Index()?
Mostafa Asaduzzaman 9-Jun-15 18:57pm    
you can have different functionalities by overloading the action. For example, Index can be Index(), Index(int id), Index(int id, Doctor doctor) etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900