वहाँ OnActionExecuting
घटना में अभी तक कोई मॉडल है। मॉडल नियंत्रक कार्रवाई द्वारा वापस किया जाता है। तो आपके पास OnActionExecuted
ईवेंट के अंदर एक मॉडल है। यही वह जगह है जहां आप मूल्य बदल सकते हैं। उदाहरण के लिए अगर हम मान लेते हैं कि अपने नियंत्रक कार्रवाई एक ViewResult लौट आए और पारित कर दिया इसे यहाँ कुछ मॉडल कैसे आप इस मॉडल कुछ संपत्ति को पुनः प्राप्त करने और संशोधित कर सकते हैं: आप दृश्य मॉडल के कुछ संपत्ति के मूल्य को संशोधित करना चाहते हैं
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as ViewResultBase;
if (result == null)
{
// The controller action didn't return a view result
// => no need to continue any further
return;
}
var model = result.Model as MyViewModel;
if (model == null)
{
// there's no model or the model was not of the expected type
// => no need to continue any further
return;
}
// modify some property value
model.Foo = "bar";
}
}
इसे क्रिया तर्क के रूप में पारित किया गया है, तो मैं इसे कस्टम मॉडल बाइंडर में करने की अनुशंसा करता हूं। लेकिन यह भी प्राप्त करना संभव है कि OnActionExecuting
घटना में:
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var model = filterContext.ActionParameters["model"] as MyViewModel;
if (model == null)
{
// The action didn't have an argument called "model" or this argument
// wasn't of the expected type => no need to continue any further
return;
}
// modify some property value
model.Foo = "bar";
}
}
स्रोत
2012-05-03 07:26:42
क्या आप समझा सकते हैं कि आपको ऐसा करने की आवश्यकता क्यों है? मुझे संदेह है कि आपको जो चाहिए उसे पूरा करने का एक बेहतर तरीका है। – DMulligan
मॉडल गुणों में से एक यह निर्धारित करता है कि दृश्य कैसा दिखाई देगा: संपादक या प्रदर्शन और मैं इसे उपयोगकर्ता अनुमतियों के अनुसार सेट करना चाहता हूं –
आप मॉडल प्रॉपर्टी पर इस तरह निर्भर क्यों कुछ रखना चाहते हैं। इसके बजाय आपको दृश्य के अंदर उपयोगकर्ता अनुमति की जांच करनी चाहिए और यह तय करना चाहिए कि कौन सा टेम्पलेट प्रस्तुत करना है (संपादक या प्रदर्शन) –