Archive for the ‘MVC’ Category

mvc – feel my pain – view model is null on postback

January 17, 2015

I just spent two to three hours because I forgot to put get/set on the public members of my ViewModel.

The “etiology” (symptoms, bad behavior) was that my form posted back but the view model was always null.

Oops!

Correct:
public int logkey { get; set;}
public string logtitle { get; set;}
public string bywhom { get; set;}
public string slogdate { get; set;}
public DateTime logdate { get; set;}
public string logentry { get; set;}

I had been copying some working code, but not this part!

Advertisements

mixed razor and html (a little beyond the simple examples you find at w3 school)

July 1, 2014

mixed razor and html (a little beyond the simple examples you find at w3 school)

(sorry about this post all in an image … I’m not skilled enough at word press to format all my bracket characters)

here is what finally worked
we took out as much razor as possible, and now I’m happy. I failed to catch another error in the image of all my travails with this – we got the asp.net system message that echoed to the screen, and the fix for that was to put in a “using” statement … and I could not get this to work in a reasonable amount of time. (less than 4-6 hours) Less razor is a good thing.


    
@Model.pagingLabelText

asp.net mvc – mixing razor code with css

June 26, 2014

1) vb syntax
2) purpose: hide/show a div from razor (I realize it’s possible to do this easily in jquery)

the page/view:
Html.BeginForm(“results”, “Results” )

@

Html.EndForm()

the css:
.dvPaging {
@Model.NotPaged()
vertical-align:text-top;
width:50%;
height: 20px;
}

the razor code:
I think it’s best to do what we need in a function.
I put presentation layer code in my view model

‘ purpose – called by the css to show or hide the paging section based on whether there are more than
‘ one page or not
Public Function NotPaged() As String
Dim sRet As String = “”
If (Not isPagination) Then sRet = “display:none; height:0px;”
Return sRet
End Function

How to post an html form to mvc

September 11, 2012

1)     The controller name isCompletionController. The URL of the form action is:

<form action=”http://localhost:36102/Completion/ReceiveCompletionForm” method=”post”>

Note that the url doesn’t contain “Controller”

2)     The controller looks like this:

[HttpPost]
public ActionResult ReceiveCompletionForm(FormCollection collection)
{

3)     I didn’t define a route – this was my default global.asax

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Note that there is nothing in the route about the FormCollection input to the controller.

mvc redirect to a controller method, uses HttpGet

August 16, 2012

I honestly don’t know if this post is great or godawful.

What I wanted to do was to have a post method redirect to another method. When you redirect, by definition, you will call the HttpGet version of a method.

What I wanted to do was:

HttpPost to FOO(Complex Object/Hashtable/FormValueCollection), and then at the end of the method, do a return RedirectToAction(“BAR”,object)

HttpGet to BAR(Complex Object/Hashtable/FormValueCollection)

kind of like this: http://stack247.wordpress.com/2011/03/20/get-forms-post-values-in-asp-net-mvc-with-formcollection/

I think it’s not possible.

But what I did get to work was:
HttpPost to FOO(Complex Object/Hashtable/FormValueCollection), and then at the end of the method, do a return RedirectToAction(“BAR”,RouteValueDictionary)

HttpGet to BAR(string param1, string param2, string ccname, string ccamt, string ccstreet, etc spelling out all parameter names in the controller definition)
and I didn’t touch the Routes.

As long as I passed a RouteValueDictionary from the first controller, and specified the names of the parameters I wanted to receive in
the second controller, my method worked.

The methods:

[HttpPost]
public ActionResult VSHcard(Models.VSHcardForm v)
{
try
{
Models.UTKAppRepository objApp = new Models.UTKAppRepository();

payment objP = null;
int iRet = objApp.VSHcardObjectToTouchnetPaymentObjectAndSave(v , ref objP, ref strError);
ViewBag.BillName = “test billname”;
ViewBag.AMT = “1234.56”;

if (iRet < 0)
{
ViewBag.lblError = strError;
}

// return View();
// try redirecting to the get version of the touchnet.create method
RouteValueDictionary dict = new RouteValueDictionary();
System.Collections.Hashtable ht = objP.get_Hashtable();

foreach (string skey in ht.Keys)
{
dict.Add(skey, ht[skey]);
}
//dict.Add(“BILL_NAME”, objP.BILL_NAME);
//dict.Add(“AMT”, objP.AMT);
//dict.Add(“ID”, 2001);

return RedirectToAction(“../Payment/Create”, dict); // HttpGet from Redirect
// we can’t send a hashtable over! return RedirectToAction(“../Payment/Create”, ht); // HttpGet from Redirect
//return RedirectToAction(“../Payment/Create”, new RouteValueDictionary(new { BILL_NAME = objP.BILL_NAME, AMT = objP.AMT })); // HttpGet from Redirect
// return RedirectToAction(“Create”, “Payment”, new RouteValueDictionary(new { BILL_NAME = objP.BILL_NAME, AMT = objP.AMT })); // HttpGet from Redirect
}
catch
{
return View();
}
}

[HttpGet]
public ActionResult Create(int? id,
string BILL_NAME,
int? UPAY_SITE_ID,
string BILL_EMAIL_ADDRESS,
string BILL_STREET1,
string BILL_STREET2,
string BILL_CITY,
string BILL_STATE,
string BILL_POSTAL_CODE,
string BILL_COUNTRY,
string EXT_TRANS_ID,
string EXT_TRANS_ID_LABEL,
decimal? AMT,
string SSV,
string SSV_PROMPT,
string VALIDATION_KEY,
string SUCCESS_LINK,
string SUCCESS_LINK_TEXT,
string ERROR_LINK,
string ERROR_LINK_TEXT,
string CANCEL_LINK,
string CANCEL_LINK_TEXT)
{
ViewBag.Message = “this is the get version of the Create method ” + id.ToString() + BILL_NAME;

// pull data out of the formCollection and put it into ViewData so the View can use it
// ref: http://stack247.wordpress.com/2011/03/20/get-forms-post-values-in-asp-net-mvc-with-formcollection/
//foreach (string _formData in formCollection.Keys)
//{
// ViewBag[_formData] = formCollection[_formData];
//}
ViewBag.BILL_NAME = BILL_NAME;
ViewBag.UPAY_SITE_ID = UPAY_SITE_ID.ToString();
ViewBag.BILL_EMAIL_ADDRESS = BILL_EMAIL_ADDRESS;
ViewBag.BILL_STREET1 = BILL_STREET1;
ViewBag.BILL_STREET2 = BILL_STREET2;
ViewBag.BILL_CITY = BILL_CITY;
ViewBag.BILL_STATE = BILL_STATE;
ViewBag.BILL_POSTAL_CODE = BILL_POSTAL_CODE;
ViewBag.BILL_COUNTRY = BILL_COUNTRY;
ViewBag.EXT_TRANS_ID = EXT_TRANS_ID;
ViewBag.EXT_TRANS_ID_LABEL = EXT_TRANS_ID_LABEL;
ViewBag.AMT = AMT.ToString();
ViewBag.SSV = SSV;
ViewBag.SSV_PROMPT = SSV_PROMPT;
ViewBag.VALIDATION_KEY = VALIDATION_KEY;
ViewBag.SUCCESS_LINK = SUCCESS_LINK;
ViewBag.SUCCESS_LINK_TEXT = SUCCESS_LINK_TEXT;
ViewBag.ERROR_LINK = ERROR_LINK;
ViewBag.ERROR_LINK_TEXT = ERROR_LINK_TEXT;
ViewBag.CANCEL_LINK = CANCEL_LINK;
ViewBag.CANCEL_LINK_TEXT = CANCEL_LINK_TEXT;

return View();

}