MVC 404 Errors - Still New - c#

So, I'm getting a 404 error on my current MVC project on submit. I'm new to MVC, so I'm likely doing something exceptionally stupid. Here's the relevant code...
<%# Page Title="Pies" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/site.master" %>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<h1>Oh Boy Pies</h1>
<p>Tell us about the pies!</p>
<form action="Process" method="post">
<div class="inputdiv">
<span class="spaced">Name:</span>
<%= Html.TextBox("name") %>
<%= Html.ValidationMessage("name", "*") %>
</div>
</form>
And the relevant handler is...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace tabdemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index ()
{
ViewData ["Message"] = "Demo!";
return View ();
}
public ActionResult Process (FormCollection form)
{
Response.Write (form ["name"]);
Response.End ();
return Redirect ("Index.aspx");
}
}
}
Also, can people explain how this would be implemented using TextBoxFor, for example? I've seen examples of it, but I don't understand it at all.
edit: Here's the masterpage
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>

it should be return RedirectToAction("Index"). MVC does not use PAGES, instead relies on Controller to route the request.
Controllers return the View, or Redirect to another Controller, which renders the view.
EDIT
And yes, the action method was incorrect(just saw)
<form action="/Home/Process" method="post">
<div class="inputdiv">
<span class="spaced">Name:</span>
<%= Html.TextBox("name") %>
<%= Html.ValidationMessage("name", "*") %>
</div>
</form>

Related

JQuery conflict with C# InnetHtml

When adding the JQuery JS include the InnetHTML is not working:
My HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="aaa.aspx.cs" Inherits="aaa" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css"
/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="ppdiv" runat="server">
</div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
</form>
</body>
</html>
My C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class aaa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ppdiv.InnerHtml = "aaa";
}
}
when include JQUery the InnetHTML is not working. Where is the error and how to fix it.
Thanks in advance
i believe it is because you are using runat="server" in head within which you add the jquery. jquery and javascript for client side, and runat="server" posts the data to server. remove runat="server" from head and check.

General Errors with ASP.NET and C#

I am new to stackoverflow and am needing help.
Last night I was working on an ASP.NET webpage using C# [I'm all very new to it] and after losing a lot of my progress after a flash drive failure, I had to rewrite from a backup I stored on google drive. After putting it onto my computer, I received problems with one of my webpages. At first it did not recognize objects "existing in the current context" but I rewrote the page from hand, because I thought it was some trouble caused by copy-pasting things back. Now I get these errors:
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends t he correct base class (e.g. Page or UserControl).
'ASP.index_aspx.GetTypeHashCode();: no suitable method found to override
'ASP.index_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override
'ASP.index_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable'
I did not have these problems at all when working with the version that I lost. Here is my code:
This is my "upload.aspx" page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
</head>
<body>
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<!-- Server-side Upload -->
<form id="form1" runat="server" style="padding-left: 2em">
<div>
<h1>Upload a File</h1>
</div>
<div>
<asp:Label ID="lblStatus1" runat="server" Text="---"></asp:Label>
<asp:FileUpload ID="fdFileDrop1" runat="server" />
<asp:Button ID="btnFileDrop1" runat="server" Text="upload" OnClick="btnFileDrop1_Click" />
</div>
</form>
</body>
</html>
And my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFileDrop1_Click(object sender, EventArgs e)
{
string strSavePath1 = "C:\\UploadBin\\";
if (fdFileDrop1.HasFile)
{
string strFileName = fdFileDrop1.FileName;
strSavePath1 += strFileName;
fdFileDrop1.SaveAs(strSavePath1);
lblStatus1.Text = "Your file was saved as " + strFileName;
}
else
{
lblStatus1.Text = "You did not specify a file to upload";
}
}
}
And because I think the bottom 3 errors are for the "index.aspx" page here is that also:
Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>BSHUpload</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
</head>
<body style="height: 228px">
<!-- Menu -->
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<ul>
<li>Home</li>
<li class="pure-menu-selected">Upload</li>
<li>Requests</li>
</ul>
</div>
<form id="form1" runat="server" style="padding-left: 2em">
</form>
<div>
<button id="button1">Testing</button>
<p id="toggle1">
Wala
</p>
<script>
$( "#button1" ).click(function() {
$( "#toggle1" ).slideToggle( "slow" );
})
</script>
</div>
</body>
</html>
Code Behind for index.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I really hope you guys can help because I'm stumped here. I'm pretty sure that both the inherits and the "System.Web.UI.Page" is correct but it says it is not.
The problem is on index.aspx. Check this at the very top of the file
CodeFile="upload.aspx.cs" Inherits="index"
That's wrong. You're pointing to the incorrect code file. It should be
CodeFile="index.aspx.cs" Inherits="index"

How To pass Field Value (like textbox) from Partial view to other Partial view

Can you help on this
I have two Partial Views in the page, the First Partial View have (Input Text and Submit Button )
Whenever the user Press Submit Button (from PartialView1 ) , I would like to ready the input Text from PartialView2
can you please help on How I can Read the Partial View1's Input Text from PartialView2 when Pressing the PartialView1's submit button ?
More specifically :reading that value from the "Action" that is rendering the PartialView2
more Details:
It is something like that : read the values from all textboxes in partialview1 and assign the all content into textbox2 in partial2 when press submit button from PartialView1
below is an example
this is my main View index.aspx:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%Html.RenderAction("ActionSenderForPartial1", "Controllername"); %>
</div>
<h2>Result</h2>
<div>
<%Html.RenderAction("ActionReceiverForpartial2", "Controllername"); %>
</div>
</body>
</html>
the PartialView 1 "Partial1"
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MvcApplication3.Models.UIField>>" %>
<form method="post">
<%:Html.TextBox("TextBox1") %>
<%:Html.TextBox("TextBox2") %>
.
.
<%:Html.TextBox("TextBoxN") %>
<p>
<button name="btnaction" value="search">Search</button>
<button name="btnaction" value="cancel">Cancel</button>
</p>
</form>
for PartialView2: "Partial2.ascx"
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%:Html.TextBox("TextBox2") %>
The actions "" & "" :
[ChildActionOnly]
public ActionResult ActionSenderForPartial1()
{
return PartialView();
}
[ChildActionOnly]
public ActionResult ActionReceiverForpartial2(string btnaction)
{
string _searchQuery = string.Empty;
--How to read the all TextBoxes contents from PartialView1 and assign the concatenated values to the TextBox2 at this partialview2
return PartialView();
}
In case there is many options I would highly appreciate to mention advising the best one as I am new in MVC.
Thanks so much
if any actual example will be highly appreciated
Please Note: i Have an idea on how to do it by Ajax but I would to do it without using Ajax .
Many Thanks
Nahed

How to trigger onclick event after received return value from popup which using add attribute a javascript?

How to trigger onclick event after received return value from popup aspx which using add attribute a javascript?
After adding
.Attributes.Add("onClick", "return popWin('" + NewBatchNo_TextBox.Text + "');");
Original onclick event do not fire?
if this method can not work, any other method to get value from pop up message box and return value and run click event
Thanks.
try this
Main.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function DoStuff() {
document.getElementById('Button1').click();
}
function popWin() {
var popy = window.open('popup.aspx', 'popup_form', 'menubar=no,status=no,top=100%,left=100;')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Show List" />
</div>
</form>
</body>
</html>
Main.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", "return popWin()");
}
}
}
popup.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="popup.aspx.cs" Inherits="popup" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function validepopupform() {
window.opener.document.getElementById('txtPopupValue').value = document.getElementById('txtPop').value
self.close();
}
window.onbeforeunload = CloseEvent;
function CloseEvent() {
if (window.opener && !window.opener.closed) {
window.opener.DoStuff();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPop" runat="server"></asp:TextBox>
<input type='button' value='go' onclick='validepopupform()' />
</div>
</form>
</body>
</html>
The same problem I faced several times the cause is that java script cannot call code behind(c#/vb function/events).
An alternate way I used is to use hidden fields that is accessible by both java script and code behind. But then you want to call events not read values modified by java script.
For this we refreshed the page through java script when we want to trigger that code behind event and let the page load to monitor the scenario(value that java script set) and triggers the required event.
The code at the end would be real mess and unmanageable, would take lots of efforts to debug.

MVC Partial Rendering with page loading optimizations and Content Placeholders

I have been trying to organise my views to optimize load times by having css in the head and scripts at the end of the body.
To this end I have created masterpages that have 3 content placeholders: styles, maincontent and scripts.
With the (achieved) aim that when I render a single view with a master this neatly can collate all the shared components and specific page components into their respective places in the rendered page. The difficultly comes when I try rendering a master-detail model.
Consider the following:
I have a Master-Detail model say Order-OrderItems and views for the Order and views for the OrderItems. In an effort to be DRY and not to repeat myself (doh! I just did right then :) ) I would like to partially render the OrderItems list view in the Order header view and still achieve the correct element layout i.e: have the any specific css be rendered into the content placeholder in the head, content into the main content placeholder and scripts into my scripts content placeholder at the bottom of the body.
I have a nasty hack at the moment but wondered if there is a cleaner way of doing this.
My current hack is the equivalent of a sql cross join with a where clause in that I do a partial render of the orderitems view in each contentplaceholder of the order view and inject the name of that placeholder into the viewdata. In the orderitems view if the placeholder name does not equal the target placeholder, I return and there by only the css get rendered into the css place holder etc. To re-phrase it, I render the child three times in the parent, while in the child I return a different part of the content for each rendering.
Smells funny and not very elegant as the child has to know that it is being rendered etc.
My next thought is to try intercepting a copy of the ViewPage and taking control of the rendering using using SetRenderMethodDelegate by only calling render on the target contentplaceholder control but this is getting very tangled at this point.
Can anyone suggest a better way that maintains code re-use and optimal html output?
Some example code:
<%# Master Language="C#" ... %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Master-Detail</title>
<!--common styles-->
<link type="text/css" href="my.css" rel="stylesheet" />
<asp:ContentPlaceHolder ID="Styles" runat="server" ></asp:ContentPlaceHolder>
</head>
<body>
<!--common markup-->
<h2>Master Markup</h2>
<asp:ContentPlaceHolder ID="mainContent" runat="server"></asp:ContentPlaceHolder>
<!--common scripts-->
<script type="text/javascript" src="/scripts/my.js"></script>
<asp:ContentPlaceHolder ID="Scripts" runat="server"></asp:ContentPlaceHolder>
</body>
</html>
Order View
<%# Page Title="Order" Language="C#" MasterPageFile="~/Views/Shared/my.Master" ... %>
<asp:Content ContentPlaceHolderID="Styles" runat="server">
<style type="text/css">
/*order styles*/
</style>
<%
var partialContext = this.Html.GetViewContext("OrderDetails", "List", this.ViewData.Model.Items, this.ViewData);
partialContext.RenderContentPlaceHolder("Styles");
%>
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<h3>Order Markup</h3>
<%
var partialContext = this.Html.GetViewContext("OrderDetails", "List", this.ViewData.Model.Items, this.ViewData);
partialContext.RenderContentPlaceHolder("MainContent");
%>
</asp:Content>
<asp:Content ContentPlaceHolderID="Scripts" runat="server">
<script type="text/javascript">
/*order specific script*/
</script>
<%
var partialContext = this.Html.GetViewContext("OrderDetails", "List", this.ViewData.Model.Items, this.ViewData);
partialContext.RenderContentPlaceHolder("Scripts");
%>
</asp:Content>
Order Items View:
<%# Page Title="OrderItems" Language="C#" MasterPageFile="my.aspx" ... %>
<asp:Content ID="Content2" ContentPlaceHolderID="Styles" runat="server">
<% if (ViewData["placeHolderName"] != null && ViewData["placeHolderName"].ToString().ToLower() != "styles")
return; %>
<style type="text/css">
/*OrderItems style*/
</style>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<% if (ViewData["placeHolderName"] != null && ViewData["placeHolderName"].ToString().ToLower() != "maincontent")
return; %>
<h4>Order Items MarkUp</h4>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Scripts" runat="server">
<% if (ViewData["placeHolderName"] != null && ViewData["placeHolderName"].ToString().ToLower() != "scripts")
return; %>
<script type="text/javascript">
/*Order Items script*/
</script>
</asp:Content>
Where there are a couple of extension methods:
public static ViewContext GetViewContext(this HtmlHelper htmlHelper, String controller, String viewName, Object model, ViewDataDictionary viewData)
{
var routeData = new System.Web.Routing.RouteData();
routeData.Values["controller"] = controller;
var controllerContext = new ControllerContext(htmlHelper.ViewContext.HttpContext, routeData, htmlHelper.ViewContext.Controller);
var viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, "Partial");
var viewData2 = new ViewDataDictionary(viewData);
viewData2.Model = model;
var viewContext = new ViewContext(controllerContext, viewEngineResult.View, viewData2, htmlHelper.ViewContext.TempData, htmlHelper.ViewContext.Writer);
return viewContext;
}
public static void RenderContentPlaceHolder(this ViewContext context, String placeHolderName)
{
context.ViewData["placeHolderName"] = placeHolderName;
context.View.Render(context, context.Writer);
context.ViewData.Remove("placeHolderName");
}
Have a look at this similar question asked some time ago:
How can I include css files from an MVC partial control?
Especially the link provided in the accepted answer is quite interesting:
http://www.codeproject.com/Articles/38542/Include-Stylesheets-and-Scripts-From-A-WebControl-.aspx
edit: You can find another blogpost of the same author updated for MVC 2 here: http://somewebguy.wordpress.com/2010/04/06/adding-stylesheets-scripts-in-asp-net-mvc2/

Categories