嗨,我刚接触mvc我试图在插入数据后重定向到编辑页面但是它重定向到Index并在Index.cshtml中使用Formmethod作为帖子并且数据被插入到数据库中并且即时通过这里插入的ID和RedirectToAction是我的控制器代码
//code for saving the data to database
[HttpPost]
public ActionResult Index(DIRECTORY_MASTER dir_create)
{
var temp = Convert.ToString(dir_create.temp);
using (var context = new material_managementEntities())
{
var temp1 = (from cs in context.CATEGORY_MASTER
where cs.CATEGORY_NAME == temp
select cs.ID).FirstOrDefault();
dir_create.CATEGORY_MASTER_ID = temp1;
}
var temp2 = Convert.ToString(dir_create.temp1);
using (var context = new material_managementEntities())
{
var temp3 = (from cs in context.REPRESENTATIVE_MASTER
where cs.REPRESENTATIVE_NAME == temp2
select cs.ID).FirstOrDefault();
dir_create.REPRESENTATIVE_MASTER_ID = temp3;
}
try
{
using (var db = new material_managementEntities())
{
db.DIRECTORY_MASTER.Add(dir_create);
db.SaveChanges();
}
int id1 = dir_create.ID;
return RedirectToAction("directoryedit","Masters", new { id = id1 });
}
catch
{
return View();
}
}
//GET:/Masters/directoryedit
public ActionResult directoryedit(int id)
{
DIRECTORY_MASTER directory_master = db.DIRECTORY_MASTER.Find(id);
var temp = directory_master.DIRECTORY_COMPETITOR;
var temp1 = directory_master.DIRECTORY_STATUS;
directory_master.status_list = Getstatus(temp1);
directory_master.competitor_list = GetYesno(temp);
return View(directory_master);
}
我的路线是
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source= (localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}