سؤال

I have an ASP.NET 4.5 web forms application using model binding. I've created a business logic layer as per this tutorial: http://www.asp.net/web-forms/tutorials/data-access/model-binding/adding-business-logic-layer

So the InsertMethod of my FormView is in the appropriate BLL class. The BLL class is defined as per above example in my OnCallingDataMethods method of the code-behind. The OnItemInserted method however, I've defined directly in my page's code-behind class, as I feel it is not business logic per say but rather front-end type logic to give the user feedback based on the completed insert.

My problem is that the AffectedRows property in the OnItemsInserted method is always -1, regardless of the Insert's result. So I can never validate whether the insert was successful or not as per this MSDN example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserted.aspx

Is the AffectedRows property faulty or not populated in the FormViewInsertedEventArgs collection when using the new model binding technique of ASP.Net 4.5 Web Forms model binding? Or am I doing something wrong?

هل كانت مفيدة؟

المحلول

My solution was a combination of above mentioned business layer checking for any model erros and DBUpdateException and then this in the ItemInserted event of the FormView:

protected void personForm_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (this.ModelState.IsValid)
        {
            labResponse.Text = "New record added. Woohoo!";
        }
        else
        {
            labResponse.Text = "";
        }
        personForm.DefaultMode = FormViewMode.Insert;
    }

This seems to work in my app and sort of covers my needs. Seems like from my testing, that if there is a model error it will never get to this event anyway, so it is safe to assume that if the modelState is valid and you've reached the OnInserted event, using EF 5 + Model Binding, your record has successfully been inserted.

We would definitely like to see more elaborate examples on MSDN and documentation on Model Binding in ASP Webforms as the current series of tutorials are very rudamentory to say the least.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top